I have the following file structure:Some directorySome file.txtAnother file here.logYet another file.mp3Another directoryWith some other file.txtFile on root level.txtAnother file on root level.extWhat I want to do now is run a little script that takes another file as input containing some type of pattern/replacement pairs in it to rename these files recursively according to them. So that every another (case insensitive) gets replaced with foo or every some with bar.I already tried a lot of things with iterating over files and reading said input file, but nothing worked like it should and I finally managed to accidentally overwrite my testing script. But there were a lot of ls, while, sed or mv in use.The two things I couldn't resolve myself were how to handle whitespace in filenames and how to not handle files that were already renamed in a previous pattern match.Maybe you can point me in the right direction?
Recursively rename files by using a list of patterns and replacements
bash;rename;recursive
TOP=`pwd -P` \find . -type d -exec sh -c '   for d   do      cd $d && \         find . ! -name . -prune -type f -exec sh -c '\''            while IFS=\; read -r pat repl            do               rename s/$pat/$repl/g $@               N=$#               for unmoved               do                  if [ -f $unmoved ]                  then                     set X ${1+$@} $unmoved                     shift                  fi               done               shift $N               case $# in 0 ) break ;; esac            done < patterns.csv         '\'' x \{\} +      cd $TOP   done' x {} +Set up find to net directories only and have sh down them in a gulp. This minimizes the number of invocations of sh.Set up find in each of these directories to net regular files, at a depth level of 1 only, and feed them to sh in a gulp. This minimizes the number of times the rename utility gets to be called.Set up a while loop to read-in the various pattern <-> replacement pairs and apply them on all the regular files.In the process of rename-ing we keep a note on whether a file was still standing after the rename process. If we find that a file still exists then that means, for some reason, it could not be renamed and hence would be tried in the next pat/repl iteration. OTOH, if the file was successfully renamed, then we DONT apply the next pat/repl iteration on this file by taking it away from the command line arguments list.