Both variations shown here using work correctly on OPs test structure:
find . -depth -name '*foo*' -execdir bash -c 'mv -i "$1" "${1//foo/bar}"' bash {} ;
or, if you have a very large number of files and want it to run faster:
find . -depth -name '*foo*' -execdir bash -c 'for f; do mv -i "$f" "${f//foo/bar}"; done' bash {} +
EDIT: As noted in the comments, my earlier answer using a find
command that did not use the execdir
option and using rename
has problems renaming files in directories that contain foo in their name. As suggested, I have changed the find commands to use -execdir
, and I have deleted the variation using the rename
command since it is a non-standard command.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…