shell 批量修改檔名

2021-06-21 06:29:52 字數 1469 閱讀 6863

當前目錄有n多個

檔案,需要按從小到大排序,然後改名成 1...n.

解法如下:(

整理來自於論壇)

方法一:

[root@squid mkdir]# ls -lrs

total 24

-rw-r--r-- 1 root root  7 jun  1 01:19 test1.txt

-rw-r--r-- 1 root root  8 jun  1 01:20 test2.txt

-rw-r--r-- 1 root root  9 jun  1 01:21 test3.txt

-rw-r--r-- 1 root root 10 jun  1 01:21 test4.txt

-rw-r--r-- 1 root root 11 jun  1 01:21 test5.txt

-rw-r--r-- 1 root root 12 jun  1 01:22 test6.txt

[root@squid mkdir]# ls -lrs|grep -v total|awk 'i+=1'|sh

[root@squid mkdir]# ll

total 24

-rw-r--r-- 1 root root  7 jun  1 01:19 1

-rw-r--r-- 1 root root  8 jun  1 01:20 2

-rw-r--r-- 1 root root  9 jun  1 01:21 3

-rw-r--r-- 1 root root 10 jun  1 01:21 4

-rw-r--r-- 1 root root 11 jun  1 01:21 5

-rw-r--r-- 1 root root 12 jun  1 01:22 6

方法二:

[root@squid mkdir]# ls -sr |

[root@squid mkdir]# ls -srl

total 24

-rw-r--r-- 1 root root  7 jun  1 01:27 1

-rw-r--r-- 1 root root  8 jun  1 01:27 2

-rw-r--r-- 1 root root  9 jun  1 01:27 3

-rw-r--r-- 1 root root 10 jun  1 01:27 4

-rw-r--r-- 1 root root 11 jun  1 01:27 5

-rw-r--r-- 1 root root 12 jun  1 01:27 6

方法三:

[root@squid mkdir]# ls -lsr|sed 1d|awk ''|sh

方法四:(

考慮檔名中包含空格的處理)

[root@squid mkdir]# ls -lsr *.* |awk ''|sh

方法六:(最精簡)

[root@squid mkdir]# ls -sr|awk ''

shell 批量修改檔名

擷取檔名的前兩個字元 fname 擷取檔案的後四個字元 bname name echo name 1 bc 拼接成檔名 filename fname name bname 更改檔名 擷取檔名的前兩個字元 fname 擷取檔案的後四個字元 bname name echo name 1 bc 拼接成檔名...

shell批量修改檔名

bin bash neox folder root folder cd folder num 0 for filename in ls folder do let num num 1 old filename prefix with suffix,new filename continuous di...

shell指令碼 批量修改檔名

參考鏈結1 shell指令碼 批量修改檔名 刪除檔名中字元 參考鏈結2 linux shell 字串操作詳解 長度,讀取,替換,擷取,連線,對比,刪除,位置 參考鏈結3 每天乙個linux命令 21 find命令之xargs 參考鏈結5 shell 學習第十天 sed 查詢與替換 批量改名,增加字元...