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腳本學(xué)習(xí)指南[一](Arnold Robbins & Nelson H.F. Beebe著)
這篇文章主要介紹了shell腳本學(xué)習(xí)指南[一]Arnold Robbins & Nelson H.F. Beebe著,需要的朋友可以參考下2014-02-02linux 開機(jī)自啟動(dòng)redis服務(wù)的方法
這篇文章主要介紹了linux 開機(jī)自啟動(dòng)redis服務(wù)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Linux?shell進(jìn)行文件解壓,復(fù)制和移動(dòng)詳解
Linux下進(jìn)行文件的解壓、復(fù)制、移動(dòng)應(yīng)該是最常見的操作了。尤其是我們?cè)陧?xiàng)目中使用大量的數(shù)據(jù)集文件時(shí)。本文我們就來(lái)細(xì)數(shù)用Shell進(jìn)行文件操作的這些坑2022-05-05shell腳本 自動(dòng)創(chuàng)建用戶詳解及實(shí)例代碼
這篇文章主要介紹了shell腳本 自動(dòng)創(chuàng)建用戶詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03shell監(jiān)控linux系統(tǒng)進(jìn)程創(chuàng)建腳本分享
shell監(jiān)控linux系統(tǒng)進(jìn)程創(chuàng)建腳本,大家參考使用吧2013-12-12Linux 按時(shí)間批量刪除文件命令(刪除N天前文件)
這篇文章主要介紹了Linux 按時(shí)間批量刪除文件的命令寫法(刪除N天前文件),需要的朋友可以參考下2017-05-05