Please consider the following command:
find . -type f -name '*.*' -exec mv '{}' '{}_foo' \;
How does find
prevent endless loops in this case?
On one hand, I believe to know that find does not work like shell globs do, i.e. it does not fetch a list of all *.jpg
files, stores that list internally and then processes the list entries. Instead, it gets the files to process "incrementally" from the underlying O/S and processes each of them as soon as it knows about it (let's ignore a certain amount of buffering which might take place since this is irrelevant to the question). After all, as far as I have understood, this is the main advantage of find
over globs in directories which have a lot of files in them.
If this is true, I would like to understand how find prevents endless loops. In the example above, 1.jpg
would be renamed to 1.jpg_foo
. From discussions on StackOverflow and elsewhere, I know that renaming might result in the file (name) occupying a different slot in the directory file list, so chances are that find encounters that file a second time, renames it again (to 1.jpg_foo_foo
), and so on.
Obviously, this does not happen.
_foo
. – Torin Jan 06 '19 at 10:38find
as well as globs) and could confirm that and tell me whyfind
is faster by orders of magnitudes in some cases. – Binarus Jan 06 '19 at 12:33find
andreaddir
, and an OS that always made new files appear at the end of the list, there could even be a deterministic infinite loop. – ilkkachu Jan 06 '19 at 13:05