1. 程式人生 > >Linux中批量替換檔名

Linux中批量替換檔名

在Linux系統中修改檔名可以用mv命令,但是它只能對單個檔案進行操作,如要要批量執行還要寫shell指令碼,用for語句迭代執行,不過Linux中另外一個命令支援批量替換檔名,它是renamerename支援正則表示式匹配。需要注意的是,rename命令在不同的Linux發行版語法格式不一樣。

語法

在Debian或者Ubuntu環境下使用的語法是:

rename 's/stringx/stringy/' files

而在CentOS下或者RedHat下是:

rename stringx stringy files

rename的引數分為三部分:

  • stringx : 被替換字串
  • stringy :替換字串
  • files :匹配的檔案列表

例如

例如有如下檔案,想把所有檔案中的@符號去掉,

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

CentOS這麼寫:

rename \@2x.png 2x.png *.png

Ubuntu這麼寫:

rename 's/\@2x/2x/' *.png

關注公眾號「Python之禪」(id:vttalk)獲取最新文章 python之禪

猜你喜歡