且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

搜索+替换文件名中的字符串

更新时间:2022-05-07 22:31:10

此处显示的两种变体在OPs测试结构上均可以正常使用:

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 {} +

编辑:如评论中所述,我之前使用不使用execdir选项并使用renamefind命令的答案在重命名包含foo的目录中的文件时遇到问题以他们的名字.如建议的那样,我已将find命令更改为使用-execdir,并且由于它是非标准命令,因此已使用rename命令删除了变体.

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.