【轉】find命令批次修改檔案字尾

shuyingxi發表於2013-05-07
一個不錯的小技巧

轉自http://space.itpub.net/?uid-28813259-action-viewspace-itemid-759748

批次修改檔案字尾名的問題你我都會用到,小結一下:

1. 首先用find命令找到滿足條件的檔案,把結果重定向到檔案old(find尋找的物件裡面包含正則匹配符號時記得外面要用單引號,否則會報錯)

[/var] # find / -type f -name '*.LOG'  > old 2> /dev/null

/root/download/readline-6.0/config.LOG
/root/download/rlwrap-0.30/config.LOG
/root/.local/share/gvfs-metadata/computer:-ce73b7ac.LOG
/root/.local/share/gvfs-metadata/trash:-2b5aca0c.LOG
/root/.local/share/gvfs-metadata/home-767242eb.LOG
/root/.local/share/gvfs-metadata/root-acf8eecb.LOG
/root/shell/apache.LOG
/root/.imsettings.LOG

2.變數迴圈讀取檔案old,使用替換方法,生成mv命令到新檔案mv,先不執行生成的mv命令(注意命令中do和done後面沒有分號)。

[/var] # for i in `cat old`;do echo mv $i ${i/.LOG/.log}; done > mv  2> /dev/null

3.檢查檔案mv內容是否正確,確認後直接執行這個檔案中的命令即可
[/var] # more mv
mv /root/download/readline-6.0/config.LOG /root/download/readline-6.0/config.log
mv /root/download/rlwrap-0.30/config.LOG /root/download/rlwrap-0.30/config.log
mv /root/.local/share/gvfs-metadata/computer:-ce73b7ac.LOG /root/.local/share/gvfs-metadata/computer:-ce73b7ac.log
mv /root/.local/share/gvfs-metadata/trash:-2b5aca0c.LOG /root/.local/share/gvfs-metadata/trash:-2b5aca0c.log
mv /root/.local/share/gvfs-metadata/home-767242eb.LOG /root/.local/share/gvfs-metadata/home-767242eb.log
mv /root/.local/share/gvfs-metadata/root-acf8eecb.LOG /root/.local/share/gvfs-metadata/root-acf8eecb.log
mv /root/shell/apache.LOG /root/shell/apache.log
mv /root/.imsettings.LOG /root/.imsettings.log

4.如果在第二步中不想儲存mv語句,直接執行mv命令,那就換如下命令
 [/var] # for i in `cat old`;do  mv $i ${i/.LOG/.log}; done 2> /dev/null | bash -x


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/438414/viewspace-760076/,如需轉載,請註明出處,否則將追究法律責任。

相關文章