bash 在bash中重命名多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15380205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename multiple files in bash
提问by andPat
I have A.js, B.js, C.jsin a certain directory and I want to write a SINGLE command line in bash shell to rename these files _A, _B, _C. How can I do this?
我有A.js, B.js,C.js在某个目录中,我想在 bash shell 中编写一个单一的命令行来重命名这些文件 _A、_B、_C。我怎样才能做到这一点?
I tried find -name '*.sh' | xargs -I file mv file basename file .shbut it doesn't work, basename file .sh isn't recognized as a nested command
我试过了,find -name '*.sh' | xargs -I file mv file basename file .sh但它不起作用,basename 文件 .sh 不被识别为嵌套命令
回答by anishsane
How about
怎么样
rename 's/(.*).js/_/' *.js
Check the syntax for rename on your system.
检查系统上的重命名语法。
The above command will rename A.jsto _A& so on.
上面的命令将重命名A.js为_A& 等等。
If you want to retain the extension, below should help:
如果您想保留扩展名,以下应该有所帮助:
rename 's/(.*)/_/' *.js
回答by Memento Mori
Assuming you still want to keep the extension on the files, you could do this:
假设您仍想保留文件的扩展名,您可以这样做:
$ for f in * ; do mv "$f" _"$f" ; done
It will get the name of each file in the directory, and prepend an "_".
它将获取目录中每个文件的名称,并在前面加上“_”。
回答by dtrckd
A simple native way to do it, with directory traversal:
一种简单的原生方式,通过目录遍历:
find -type f | xargs -I {} mv {} {}.txt
Will rename every file in place adding extension .txt at the end.
将重命名每个文件,在末尾添加扩展名 .txt。
And a more general cool way with parallelization:
还有一种更通用的并行化方式:
find -name "file*.p" | parallel 'f="{}" ; mv -- {} ${f:0:4}change_between${f:8}'

