如何使用-exec查找和重命名所有文件

时间:2017-08-13 19:32:25

标签: linux find pipe exec rename

我想重命名所有以“.mp4”结尾的文件,而不是使用空格来包含下划线。 例如:

Original file -> test 1.mp4
Renamed file -> test_1.mp4

我正在尝试:

find . -iname "*.mp4"  -exec mv {} $(echo '{}' | tr " " "_") \;

但我只得到:

mv: ‘./test 1.mp4’ and ‘./test 1.mp4’ are the same file

似乎我的烟斗不起作用。我会很感激所有的想法。

2 个答案:

答案 0 :(得分:-1)

我会考虑使用重命名命令,假设您的发行版提供了它。在Debian中:https://packages.debian.org/rename

如果您不需要在子目录中搜索:

rename 's/\ /_/' *.mp4

如果你这样做:

find . -iname "*.mp4" -execdir rename 's/\ /_/' {} \;

如果必须替换多个空格,则搜索/替换表达式将变为's/\ /_/g'

如果你真的想保留你的语法/方法,请看这个非常相似的问题:find -exec with multiple commands

答案 1 :(得分:-1)

到目前为止,唯一的方法是通过“重命名”命令。

这是有效的命令:

find /tmp/test/ -iname "*.mp4" -exec rename " " "_" "{}" \;