Shell腳本批量添加擴(kuò)展名的兩種方法分享
方法1:
for file in `ls`; do mv $file $file.txt; done
方法2:
find . -type f |xargs -i mv {} {}.txt
還有一些試驗(yàn)不成功的,先記錄在此。
1.用rename命令修改后綴名,這個(gè)是最簡(jiǎn)單最省事的辦法
[root@demo test_rename]# ll
總計(jì) 20
-rw-r–r– 1 root root 0 09-27 00:57 rename1.log
-rw-r–r– 1 root root 0 09-27 00:57 rename2.log
-rw-r–r– 1 root root 0 09-27 00:57 rename3.log
-rw-r–r– 1 root root 0 09-27 00:57 rename4.log
-rw-r–r– 1 root root 0 09-27 00:57 rename5.log
[root@demo test_rename]# rename log txt *.log #把*.log改為*.txt
[root@demo test_rename]# ll
總計(jì) 20
-rw-r–r– 1 root root 0 09-27 00:57 rename1.txt
-rw-r–r– 1 root root 0 09-27 00:57 rename2.txt
-rw-r–r– 1 root root 0 09-27 00:57 rename3.txt
-rw-r–r– 1 root root 0 09-27 00:57 rename4.txt
-rw-r–r– 1 root root 0 09-27 00:57 rename5.txt
[root@demo test_rename]#
2.用for、sed和mv修改后綴名
[root@demo test_rename]# ll
總計(jì) 20
-rw-r–r– 1 root root 0 09-27 01:51 rename1.log
-rw-r–r– 1 root root 0 09-27 01:21 rename2.log
-rw-r–r– 1 root root 0 09-27 01:21 rename3.log
-rw-r–r– 1 root root 0 09-27 01:21 rename4.log
-rw-r–r– 1 root root 0 09-27 01:21 rename5.log
[root@demo test_rename]# for i in $(ls .)
> do
> mv $i $(echo $i|sed ‘s/\.log/\.txt/')
> done
[root@demo test_rename]# ll
總計(jì) 20
-rw-r–r– 1 root root 0 09-27 01:51 rename1.txt
-rw-r–r– 1 root root 0 09-27 01:21 rename2.txt
-rw-r–r– 1 root root 0 09-27 01:21 rename3.txt
-rw-r–r– 1 root root 0 09-27 01:21 rename4.txt
-rw-r–r– 1 root root 0 09-27 01:21 rename5.txt
[root@demo test_rename]#
3.用find和xargs添加后綴名
[root@demo test_rename]# ll
總計(jì) 20
-rw-r–r– 1 root root 0 09-27 02:20 rename1
-rw-r–r– 1 root root 0 09-27 02:20 rename2
-rw-r–r– 1 root root 0 09-27 02:20 rename3
-rw-r–r– 1 root root 0 09-27 02:20 rename4
-rw-r–r– 1 root root 0 09-27 02:20 rename5
[root@demo test_rename]# find . -type f |xargs -i mv {} {}.txt
[root@demo test_rename]# ll
總計(jì) 20
-rw-r–r– 1 root root 0 09-27 02:20 rename1.txt
-rw-r–r– 1 root root 0 09-27 02:20 rename2.txt
-rw-r–r– 1 root root 0 09-27 02:20 rename3.txt
-rw-r–r– 1 root root 0 09-27 02:20 rename4.txt
-rw-r–r– 1 root root 0 09-27 02:20 rename5.txt
[root@demo test_rename]#
相關(guān)文章
shell函數(shù)內(nèi)調(diào)用另一個(gè)函數(shù)(不帶返回值和帶返回值)
本文主要介紹了shell函數(shù)內(nèi)調(diào)用另一個(gè)函數(shù)(不帶返回值和帶返回值),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
在Linux下用scp復(fù)制文件無(wú)需輸入密碼的技巧
在Linux環(huán)境下,兩臺(tái)主機(jī)之間傳輸文件一般使用scp命令,通常用scp命令通過(guò)ssh獲取對(duì)方linux主機(jī)文件的時(shí)候都需要輸入密碼確認(rèn)。通過(guò)建立信任關(guān)系,可以實(shí)現(xiàn)不輸入密碼,感興趣的朋友跟著小編一起學(xué)習(xí)在Linux下用scp復(fù)制文件無(wú)需輸入密碼的技巧2015-09-09
Linux中執(zhí)行shell腳本的4種方法總結(jié)
這篇文章主要介紹了Linux中執(zhí)行shell腳本的4種方法總結(jié),即在Linux中運(yùn)行shell腳本的4種方法,需要的朋友可以參考下2014-08-08
linux awk時(shí)間計(jì)算腳本及awk命令詳解
這篇文章主要介紹了linux awk時(shí)間計(jì)算腳本及awk命令詳解的相關(guān)資料,需要的朋友可以參考下2015-11-11

