2015-05-11 29 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