3

So if I am downloading say 5000 audio files, each 5 seconds long, and after the first 1000 download, I run:

find . -iname "*.wav" -exec play {} \;

Will that play only the 1000 files that were present when I first ran the command, or will it play all 5000 files since the files are downloading faster than the exec finishes with them?

1

This depends on the implementation, because the standard says:

If a file is removed from or added to the directory hierarchy being searched it is unspecified whether or not find includes that file in its search.

I think either behaviour may happen, depending on the directory layout and the filesystem behaviour. If, after find opens a directory for reading its entries, newly added entries are returned by readdir, then find will process that too. But that's also unspecified by POSIX:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.


At least with GNU find and BSD find on macOS, the behaviour seems to be to read all entries of a directory at once:

% (sleep 5; touch abc) & find . -exec sleep 5 \; -print
[1] 57727
.
[1]  + 57727 done       ( sleep 5; touch abc; )
./foo
./bar

Note how it didn't find abc. But if the first sleep is decreased so that find recurses into . after that sleep is over:

% rm abc; (sleep 2; touch abc) & find . -exec sleep 5 \; -print
[1] 57971
[1]  + 57971 done       ( sleep 2; touch abc; )
.
./abc
./foo
./bar

So, if your files are being downloaded into different subdirectories, some files that were downloaded after find was run may get played, but some other files may not.

| improve this answer | |
  • The only find implementation that AFAIK reads directories "at once" is libfind from schilytools. Do not confuse blocked directory reading with bulk reading. The mistake you made is to use far too few files for your test. – schily Nov 21 '19 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.