2016-11-29 110 views
-1

我有一个文件夹FOLDER1,其中包含不同的文件。通过bash更改特定文件夹中的文件名

我的文件夹中的几个文件的扩展名png格式

我想改变这一切与扩展用bash脚本png格式文件的文件名。我试图写一篇,但我仍然没有达到我想要的。

#!/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 

在脚本结束时,我想都命名为喜欢的文件:

C-1.png

C-2.png

C-3。 PNG

...

...

...

你能帮我吗? 感谢

+0

http://stackoverflow.com/questions/18686832/rename-all-files-in-folder-to-numbered-list-1-jpg-2-jpg –

回答

1

对不起,我发现我的解决方案。

此代码工作正常。

#!/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 
1

首先需要注意的是:

i+=1 

是字符串相加。你在做什么是0,01,011,0111 ....你需要:

((++i)) 

接下来,你需要分割你的文件名,如果一条路“”只出现一次:

base=$(echo $f | cut -d. -f1) 

终于动:

mv $f ${base}-${i}.png 
1
#!/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 
+0

是的,我抵达相同的结论。感谢所有的答复。 – kalmanIsAGameChanger

相关问题