简体   繁体   English

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

[英]How to find and rename all files using -exec

I want to rename all files which end on ".mp4" in such way that instead of white spaces to contain underscores. 我想重命名所有以“ .mp4”结尾的文件,而不是用空格包含下划线。 Example: 例:

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

I was trying with: 我正在尝试:

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

But I got only: 但是我只有:

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

It seems that my pipe is not working.I would appreciate all ideas. 看来我的烟斗没用,我很感谢所有想法。

I'd look into using the rename command, assuming your distribution provides it. 我会考虑使用重命名命令,假设您的发行版提供了它。 In Debian: https://packages.debian.org/rename 在Debian中: https//packages.debian.org/rename

If you don't need to search in subdirectories: 如果您不需要在子目录中搜索:

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

If you do: 如果您这样做:

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

If multiple spaces must be replaced, the search/replace expression becomes 's/\\ /_/g' 如果必须替换多个空格,则搜索/替换表达式变为's/\\ /_/g'

If you really want to keep your syntax/approach, see this very similar question: find -exec with multiple commands 如果您确实想保留自己的语法/方法,请参见以下非常相似的问题: 使用多个命令查找-exec

It seems that the only way to do it so far is via "rename" command. 到目前为止,似乎唯一的方法是通过“重命名”命令。

Here is the command that worked: 这是有效的命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM