如何修改这个`while read; mv printf` 代码仅重命名某些文件类型的文件?

Ano*_*ous 2 ls bash shell-script rename printf

有问题的代码:

ls | cat -n | while read n f; do mv "$f" `printf "video_%03d.mp4" $n`; done
Run Code Online (Sandbox Code Playgroud)

上面的代码将执行的目录中的所有文件/文件夹重命名为:

video_001.mp4
video_002.mp4
video_003.mp4
and so on... 
Run Code Online (Sandbox Code Playgroud)

但是,最好只针对特定类型的文件,例如只重命名.mp4文件,而不是重命名包括目录在内的所有内容。

Ste*_*itt 11

这将重命名所有.mp4文件,而不解析ls

i=1; for file in *.mp4; do mv "$file" $(printf "video_%03d.mp4" "$i"); i=$((i + 1)); done
Run Code Online (Sandbox Code Playgroud)

任何 glob 都可以在for file in ...语句中使用。您可以通过 usingfind来应用其他标准。