bash 将目录中的所有文件从 $filename_h 重命名为 $filename_half?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7450818/
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 all files in directory from $filename_h to $filename_half?
提问by Richard
Dead simple.
死的简单。
How do I rename
我如何重命名
05_h.png
06_h.png
to
到
05_half.png
06_half.png
At least, I think it's simple, but it's hard to Google for this kind of thing unless you already know.
至少,我认为这很简单,但除非你已经知道,否则很难用谷歌搜索这种东西。
Thanks....
谢谢....
回答by bash-o-logist
Just use bash, no need to call external commands.
只需使用 bash,无需调用外部命令。
for file in *_h.png
do
mv "$file" "${file/_h.png/_half.png}"
done
Do not add #!/bin/sh
不添加 #!/bin/sh
For those that need that one-liner:
对于那些需要单线的人:
for file in *.png; do mv "$file" "${file/_h.png/_half.png}"; done
回答by Micha? ?rajer
Try rename
command:
尝试rename
命令:
rename 's/_h.png/_half.png/' *.png
Update:
更新:
example usage:
用法示例:
create some content
创建一些内容
$ mkdir /tmp/foo
$ cd /tmp/foo
$ touch one_h.png two_h.png three_h.png
$ ls
one_h.png three_h.png two_h.png
test solution:
测试解决方案:
$ rename 's/_h.png/_half.png/' *.png
$ ls
one_half.png three_half.png two_half.png
回答by Karoly Horvath
for f in *.png; do
fnew=`echo $f | sed 's/_h.png/_half.png/'`
mv $f $fnew
done
回答by sorpigal
Are you looking for a pure bash solution? There are many approaches, but here's one.
您在寻找纯 bash 解决方案吗?有很多方法,但这里有一个。
for file in *_h.png ; do mv "$file" "${file%%_h.png}_half.png" ; done
This presumes that the only files in the current directory that end in _h.png
are the ones you want to rename.
这假定当前目录中唯一以 结尾的文件_h.png
是您要重命名的文件。
Much more specifically
更具体的
for file in 0{5..6}_h.png ; do mv "$file" "${file/_h./_half.}" ; done
Presuming those two examples are your only. files.
假设这两个例子是你唯一的例子。文件。
For the general case, file renaming in hasbeencoveredbefore.
回答by Fredrik Pihl
Use the rename
utility written in perl.
Might be that it is not available by default though...
使用rename
用 perl 编写的实用程序。可能是默认情况下它不可用...
$ touch 0{5..6}_h.png
$ ls
05_h.png 06_h.png
$ rename 's/h/half/' *.png
$ ls
05_half.png 06_half.png
回答by James Yen
I had a similar question: In the manual, it describes rename as
我有一个类似的问题:在手册中,它描述了重命名为
rename [option] expression replacement file
so you can use it in this way
所以你可以这样使用它
rename _h _half *.png
In the code: '_h' is the expression that you are looking for. '_half' is the pattern that you want to replace with. '*.png' is the range of files that you are looking for your possible target files.
在代码中: '_h' 是您要查找的表达式。'_half' 是您要替换的模式。'*.png' 是您正在寻找可能的目标文件的文件范围。
Hope this can help c:
希望这可以帮助 c:
回答by ztank1013
for i in *_h.png ; do
mv $i `echo "$i"|awk -F'.' '{print "alf."}'`
done
回答by Easwar
Another approach can be manually using batch rename option
另一种方法可以手动使用批量重命名选项
Right click on the file -> File Custom Commands -> Batch Rename and you can replace h. with half.
右键单击文件-> 文件自定义命令-> 批量重命名,您可以替换h。与一半。
This will work for linux based gui using WinSCP etc
这将适用于使用 WinSCP 等的基于 linux 的 gui
回答by Jadeye
One liner:for file in *.php ; do mv "$file" "_$file" ; done
一个班轮:for file in *.php ; do mv "$file" "_$file" ; done
回答by C. Ramseyer
Use the rename
utility:
使用该rename
实用程序:
rc@bvm3:/tmp/foo $ touch 05_h.png 06_h.png
rc@bvm3:/tmp/foo $ rename 's/_h/_half/' *
rc@bvm3:/tmp/foo $ ls -l
total 0
-rw-r--r-- 1 rc rc 0 2011-09-17 00:15 05_half.png
-rw-r--r-- 1 rc rc 0 2011-09-17 00:15 06_half.png