2016-07-30 38 views
1

我想在我的Linux系統目錄重命名多個文件....如何使用字符串替換來重命名linux中某個目錄中的所有文件?

我的文件名如:

Lec 1 - xxx.webm 
Lec 2 - xxx.webm 
Lec 3 - xxx.webm 
Lec 4 - xxx.webm 

和這樣的例子不勝枚舉...

這裏XXX可能是字符(不一致)的任何名單....

我想每個文件在這裏重新命名,如:

mv Lec 1 - xxx.webm Lec 1.webm 
mv Lec 2 - xxx.webm Lec 2.webm 
mv Lec 3 - xxx.webm Lec 3.webm 

等等......

for循環可以做但如何做替換?

*去除所有字符後的數字應該是我的重命名的文件

回答

2

for循環應該做的工作:

for f in *.webm; do 
    mv "$f" "${f/ -*/}.webm" 
done 
+0

文件名改爲'LEC N - webm.webm' – coolstoner

+0

無'$ {F/- * /}'將在原始字符串的「」 - 「'後刪除所有內容,然後將添加」.webm「。 – anubhava

+0

現在我所有的文件都被重新命名爲'Lec n.webm.webm' 你能幫我重新命名爲'Lec n.webm'嗎? 我試過這個鏈接,但我得到錯誤 http://unix.stackexchange.com/questions/102647/how-to-rename-multiple-files-in-single-command-or-script-in-unix – coolstoner

1

${string%substring}:刪除從$string$substring最短的匹配。

for i in *.webm; do mv $i ${i%xxx}; done 

或檢查出:

${string%%substring}:刪除從$string$substring最長匹配。

0

如果您已經安裝util-linux-ng

find . -name "Lec*.webm" | xargs rename s/ -*// 

或:

for file in $(find . -name "Lec*.webm") 
do 
    echo mv $file `echo $file | sed s/ -*$//` 
done 
相關問題