bash 给目录下所有文件添加后缀名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24592342/
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
Add suffix to all files in the directory with an extension
提问by user1757703
How to add a suffix to all files in the current directory in bash?
如何在bash中为当前目录中的所有文件添加后缀?
Here is what I've tried, but it keeps adding an extra .png
to the filename.
这是我尝试过的,但它不断向.png
文件名添加一个额外的内容。
for file in *.png; do mv "$file" "${file}_3.6.14.png"; done
回答by Barmar
for file in *.png; do
mv "$file" "${file%.png}_3.6.14.png"
done
${file%.png}
expands to ${file}
with the .png
suffix removed.
${file%.png}
扩展到${file}
与.png
后缀去掉。
回答by Avinash Raj
You could do this through rename command,
你可以通过重命名命令来做到这一点,
rename 's/\.png/_3.6.14.png/' *.png
Through bash,
通过 bash,
for i in *.png; do mv "$i" "${i%.*}_3.6.14.png"; done
It replaces .png
in all the .png
files with _3.6.14.png
.
它.png
在所有.png
文件中替换为_3.6.14.png
.
${i%.*}
Anything after last dot would be cutdown. So.png
part would be cutoff from the filename.mv $i ${i%.*}_3.6.14.png
Rename original .png files with the filename+_3.6.14.png.
${i%.*}
最后一个点之后的任何内容都会被删减。所以.png
部分将从文件名中截断。mv $i ${i%.*}_3.6.14.png
使用文件名+_3.6.14.png 重命名原始 .png 文件。