2011-08-31 79 views

回答

18

在外殼上,你可以這樣做:

for file in *; do 
    if [ -f ${file} ]; then 
     mv ${file} ${file}.xml 
    fi 
done 

編輯

要做到這一點遞歸所有子目錄,你應該使用find

for file in $(find -type f); do 
    mv ${file} ${file}.xml 
done 

另如果你要做比這更復雜的任何事情,你可能不應該使用shell腳本。

更妙

使用由以下Jonathan Leffler提供的評論:

find . -type f -exec mv {} {}.xml ';' 
+0

我怎麼做遞歸的所有子目錄 – aWebDeveloper

+5

使用'find'找到文件; '找。 -type f -exec mv {} {} .xml';''。 –

+1

更新了答案。 –

2

的Python

使用os.listdir找到目錄中的所有文件的名稱。如果您需要遞歸地查找子目錄中的所有文件,請改爲使用os.walk。它的API比os.listdir更復雜,但它提供了遞歸遍歷目錄的強大方法。

然後使用os.rename重命名文件。

+0

一些示例代碼或示例或鏈接。我不知道python – aWebDeveloper

+0

@Web開發人員:你剛剛-1爲我提供答案嗎?真的嗎? –

+0

不,我錯誤地點擊它,我立即撤消它 – aWebDeveloper

3

不知道這是否是標準的,但我的Perl包(於Debian/Ubuntu)包括/usr/bin/prename(和一個符號鏈接只是rename)沒有其他目的:

rename 's/$/.xml/' * 
+0

我如何在所有子目錄上遞歸地執行它,但不在.xml文件上。即沒有重命名爲 – aWebDeveloper

+0

的文件使用'$(find -type f)'而不是'*'。並使用斷言's /(?<!\。xml)$ /。xml /' – mario

+2

來修改正則表達式。您可以使用帶-n標誌的重命名來測試重命名的方式,而不實際重命名文件。 –

3
find . -type f \! -name '*.xml' -print0 | xargs -0 rename 's/$/.xml/' 
相關問題