更改脚本重命名从生成A0 A1 A2 A3到A001 A002 A003

时间:2013-01-04 04:21:44

标签: linux shell rename

如何将文件从A0 A1 A2 A3重命名为A001 A002 A003 我有一个这样的重命名代码:

n=$(ls -1 | grep '^A' -c)             # find the number for A files
nfiles=$(ls -1 *.obj| grep -v '^A');  # list of all non-A files
for file in $nfiles;                  # for each of the non-A files
do
n=$(($n+1));
new_file=A$n.obj                   # new name with next available A file name
mv $file $new_file                 # rename with new file name
done

2 个答案:

答案 0 :(得分:1)

进行一些更改:

n=$(ls [A]* | wc -l)
nfiles=$(ls [^A]*.obj)               # list of all non-A files
for file in $nfiles;                 # for each of the non-A files
do
  n=$(($n+1));
  new_file=$(printf "A%03d.obj" $n)  # new name with next available A file name
  echo mv $file $new_file            # rename with new file name
done

答案 1 :(得分:0)

如果原始文件是另一个名称,例如。 ant.obj dog.obj,如何更改代码重命名为A001 A002 A003 ....