Issue
I used the following command to add a prefix to all filenames in the current directory:
find . -maxdepth 1 -type f | xargs -I {} basename {} | xargs -I {} mv {} prefix-{}
My question is: is this procedure safe with GNU find? Or could the find process, which may still be running after the first files are renamed, find the renamed files again, such that they get the prefix multiple times?
I used the same command to rename the same files with busybox and found that some of them got the prefix multiple times. This is perfectly understandable, but since I never had the problem with GNU find (GNU findutils) 4.7.0, I'm wondering if the GNU variant has some protection against this outcome.
I looked at the documentation and tried to find the answer with Google, but I could not find anything relevant.
Solution
Following experiment proves it can happen to GNU find :
#!/usr/bin/env bash
dir=/tmp/find
rm -rf $dir; mkdir $dir; cd $dir
for i in {1..120000}; do touch $i; done # Create 120000 empty files.
find . -maxdepth 1 -type f -printf "%P\n" | xargs -I {} mv {} prefix-{}
find . -name "prefix-prefix*" | wc # gives -> 14546 14546 319622
find --version # gives -> find (GNU findutils) 4.7.0
uname -a # gives -> Linux ubuntu 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
See wc
result.
Answered By - Philippe Answer Checked By - Katrina (WPSolving Volunteer)