2015-05-11 28 views
0

我正在運行OSX優勝美地。如何批量重新排列OSX中的文件名?

我有一批這種格式命名的文件: XX-YYYab.txt

的XX是可變的數字,因爲是YYY。 'a'和'b'是可變字母。

我想重新排列的文件名來獲得YYY-XXab.txt,這樣我可以通過YYY組件進行排序。

+0

向我們展示你有什麼至今。 – trojanfoe

+0

您可能正在尋找'rename'。 –

+0

到目前爲止,我剛剛在優勝美地使用了Finder的'重命名'工具來刪除一些預先設置的文本。但該界面不支持通配符。 – Ryan

回答

0

我會做這樣的終端 - 在你的文件的副本的情況下,第一次運行它,它則會覆蓋的東西。確保在運行之前將目錄更改爲文件的位置。

保存下列文件作爲renamethem

#!/bin/bash 
# Loop through all files that start with digits, have dashes, and end in ".txt" 
for f in [0-9]*-*.txt;do 

    # Get xx - the leading digits before a dash 
    xx=${f/-*/} 

    # Get yy - the digits after the dash 
    yy=$(sed -E 's/.*-([0-9]*).*/\1/' <<< $f) 

    # Get the letters before ".txt" 
    zz=$(sed -E 's/.*[0-9](.*)\.txt/\1/' <<< $f) 

    # DEBUG echo $xx, $yy, $zz 

    # Remove the word "echo" below to do the actual rename 
    echo mv "$f" "${yy}-${xx}${zz}.txt" 
done 

然後執行以下操作只是一次使其可執行

chmod +x renamethem 

然後運行它通過鍵入

/.renamethem 

樣本輸出

mv 12-345ab.txt 345-12ab.txt 
mv 333333-9999axrt.txt 9999-333333axrt.txt 
mv 45-3rm.txt 3-45rm.txt