如何使用shell脚本重命名多个文件? [重复]

时间:2023-01-13 15:55:12

This question already has an answer here:

这个问题在这里已有答案:

I am a beginner at shell scripting. I have 4 images:

我是shell脚本的初学者。我有4张图片:

 1.png, 2.png, 3.png, 4.png 

How do I rename these images to:

如何将这些图像重命名为:

img1.png, img2.png, img3.png, img4

3 个解决方案

#1


Using perl based rename:

使用基于perl的重命名:

rename 's/^/img/' *.png

#2


Use this as a script with your file names as input. It is untested, but should give you a clue

将此作为脚本,将文件名作为输入。它未经测试,但应该给你一个线索

#! /bin/bash    
for file in "$@"; do
     mv "$file" "img${file}"
done

#3


Have a look for the rename command, you can do something like

看看重命名命令,你可以做类似的事情

rename s/^/img/g *png

This substitutes (s/) the beginning of a file name (noted as the ^) with img for all files ending in png (*png)

对于以png结尾的所有文件(* png),用img替换(s /)文件名的开头(标记为^)

.if you don't have it, you can get the command from here http://*.org/wiki/Rename.pl

。如果你没有它,你可以从这里获得命令http://*.org/wiki/Rename.pl

for instance

#1


Using perl based rename:

使用基于perl的重命名:

rename 's/^/img/' *.png

#2


Use this as a script with your file names as input. It is untested, but should give you a clue

将此作为脚本,将文件名作为输入。它未经测试,但应该给你一个线索

#! /bin/bash    
for file in "$@"; do
     mv "$file" "img${file}"
done

#3


Have a look for the rename command, you can do something like

看看重命名命令,你可以做类似的事情

rename s/^/img/g *png

This substitutes (s/) the beginning of a file name (noted as the ^) with img for all files ending in png (*png)

对于以png结尾的所有文件(* png),用img替换(s /)文件名的开头(标记为^)

.if you don't have it, you can get the command from here http://*.org/wiki/Rename.pl

。如果你没有它,你可以从这里获得命令http://*.org/wiki/Rename.pl

for instance