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

标签 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

最佳答案

有一些变化:

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

关于linux - 更改脚本重命名从生成 A0 A1 A2 A3 到 A001 A002 A003,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14151043/

相关文章:

c - gcc 4.6.3 中的 libm 链接问题

linux - ASP.NET Core 2 Web API - 502 Bad Gateway(Centos 7) - NGINX

bash - 在 "for"循环 bash 中解析多个 awk 变量

Powershell 异常 : Not enough quota is available to process this command

Java - renameTo 方法不起作用

linux - 为什么路径命令在 Docker 文件中不起作用

c++ - 大型递归字符串操作的段错误

java - 通过带有环境变量的 Java SSH

mysql - 在shell脚本中将字符串转换为xml?

r - 重命名列表项