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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:48:15  来源:igfitidea点击:

Add suffix to all files in the directory with an extension

bash

提问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 .pngto 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 .pngsuffix 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 .pngin all the .pngfiles with _3.6.14.png.

.png在所有.png文件中替换为_3.6.14.png.

  • ${i%.*}Anything after last dot would be cutdown. So .pngpart would be cutoff from the filename.
  • mv $i ${i%.*}_3.6.14.pngRename original .png files with the filename+_3.6.14.png.
  • ${i%.*}最后一个点之后的任何内容都会被删减。所以.png部分将从文件名中截断。
  • mv $i ${i%.*}_3.6.14.png使用文件名+_3.6.14.png 重命名原始 .png 文件。