[英]Change filenames in a specific folder through bash
I have a folder FOLDER1 with different files in it. 我有一个文件夹FOLDER1,其中包含不同的文件。
I have several files in the folder with an extension .png 我的文件夹中有几个扩展名为.png的文件
I would like to change the filename of all the files with extension .png with a bash script. 我想使用bash脚本更改所有扩展名为.png的文件的文件名。 I tried to write one but I still didn't arrive to have what I want.
我试图写一个,但是我仍然没有得到想要的东西。
#!/bin/bash
# make sure you always put $f in double quotes to avoid any nasty surprises i.e. "$f"
i=0
for f in *.png
do
echo "${i}Processing $f file..."
i+=1;
echo ${i}
# rm "$f"
done
At the end of the script I would like to have all the files named like: 在脚本末尾,我想将所有文件命名为:
c-1.png c-1.png
c-2.png c-2.png
c-3.png c-3.png
... ...
... ...
... ...
Could you help me? 你可以帮帮我吗? Thanks
谢谢
First note that: 首先要注意的是:
i+=1
is string addition. 是字符串加法。 What you're doing is 0,01,011,0111.... You need:
您正在做的是0,01,011,0111 ....您需要:
((++i))
Next you need to split your file name, one way if "." 接下来,您需要分割文件名,如果是“。”,则是一种方式。 appears only once:
仅出现一次:
base=$(echo $f | cut -d. -f1)
and finally move: 最后移动:
mv $f ${base}-${i}.png
Sorry, I found my solution . 抱歉,我找到了解决方案。
This code is working perfectly. 该代码运行良好。
#!/bin/bash
# make sure you always put $f in double quotes to avoid any nasty surprises i.e. "$f"
i=0
for f in *.png
do
echo "$i Processing $f file..."
i=$((i+1))
mv $f "c-"$i.png
#echo ${i}
done
#!/bin/bash
i=0
for f in *.png
do
echo "${i}Processing $f file..."
i=$((i + 1))
newname="c-${i}.png"
mv "$f" $newname
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.