0

I have a directory with many files. If I move a file to a subdirectory by mv myfile.* ./subdirectory/, I have a delay of about 3 seconds. Then the files are moved.

How can I avoid this delay?


The background is, that I want to sort this directory. So I have written a script to check the content of a configuration file and move the configuration file plus 2 other files to a special subdirectory.

10
  • Are you saying there's a delay first where nothing happens, and then the files get moved all at once? That would sound a bit surprising, as pretty much the only thing that happens beforehand is the shell fetching the list of filenames to pass to mv. The moves themselves would necessarily happen one-by-one.
    – ilkkachu
    Oct 9, 2023 at 12:20
  • 2
    How many files are "many"?
    – Kusalananda
    Oct 9, 2023 at 12:29
  • Is this on a local filesystem or a remotely mounted one? Oct 9, 2023 at 12:36
  • 1
    find -maxdepth 1 -printf . | wc -c should give you the number of files/directories +1 Oct 9, 2023 at 14:04
  • 1
    find -maxdepth 1 -printf . | wc -c will give you one more than the number of files/directories. And find -maxdepth 1 -type f -printf . | wc -c will give you the precise number of files (without directories) Oct 9, 2023 at 17:18

1 Answer 1

2

If you have hundreds of thousands of files or more, then the time you see is the time it takes your shell to identify all possible expansions of the glob myfile.*. That means it looks for all file names starting with myfile. and then any number of any other character. So when you have very many files, that means very many files to check and some lag.

To avoid the lag, use the actual file name: mv myfile.whatever ./subdirectory/. If that isn't an option, and you need to find the file names on the fly, then either start using more subdirectories so none of your directories has too many files, or just learn to live with the wait.

1
  • Maybe it's better to leave this alone. It was just an idea to handle an chaos (thats not really mine). But thank you though!
    – Andy A.
    Oct 9, 2023 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .