夔牛犇犇 2019-03-15 15:53 采纳率: 100%
浏览 1215
已采纳

如何批量修改数字文件名

现在有以数字命名的文件如下:
-rw-r--r-- 1 root root 0 Mar 15 15:45 12.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 13.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 14.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 15.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 16.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 17.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 18.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 19.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 1.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 2.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 3.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 4.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 5.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 6.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 7.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 8.txt
-rw-r--r-- 1 root root 0 Mar 15 15:45 9.txt

想将所有文件重新命名,就是在原来的文件名前加一个a变成a1.txt这类的,
如果在数字连续的情况下,可以用FOR循环来实现:

[root@centos_7_1 dd]# i=1; for x in *; do mv $x a$i.txt;let i=i+1; done

但是现在数字文件名不连续,该怎么做?

  • 写回答

2条回答 默认 最新

  • mkchiny 2019-03-15 16:03
    关注
    #!/bin/bash
    
    for file in `ls`
    do
        mv ${file} a${file}
    done
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?