2010-11-22 108 views
1

編輯:好吧,對不起,我應該指定我在Windows上,並使用基於bash 1.14.2的win-bash以及gnuwin32工具。這意味着所有發佈的解決方案都不起作用。它不包含許多高級功能。然而,我終於明白了。這是一個醜陋的腳本,但它的作品。遞歸BASH重命名

#/bin/bash 
function readdir 
{ 
    cd "$1" 
    for infile in * 
    do 
    if [ -d "$infile" ]; then 
     readdir "$infile" 
    else 
     renamer "$infile" 
    fi 
    done 
    cd .. 
} 

function renamer 
{ 
    #replace " - " with a single underscore. 
    NEWFILE1=`echo "$1" | sed 's/\s-\s/_/g'` 
    #replace spaces with underscores 
    NEWFILE2=`echo "$NEWFILE1" | sed 's/\s/_/g'` 
    #replace "-" dashes with underscores. 
    NEWFILE3=`echo "$NEWFILE2" | sed 's/-/_/g'` 
    #remove exclamation points 
    NEWFILE4=`echo "$NEWFILE3" | sed 's/!//g'` 
    #remove commas 
    NEWFILE5=`echo "$NEWFILE4" | sed 's/,//g'` 
    #remove single quotes 
    NEWFILE6=`echo "$NEWFILE5" | sed "s/'//g"` 
    #replace & with _and_ 
    NEWFILE7=`echo "$NEWFILE6" | sed "s/&/_and_/g"` 
    #remove single quotes 
    NEWFILE8=`echo "$NEWFILE7" | sed "s/’//g"` 

    mv "$1" "$NEWFILE8" 
} 

for infile in * 
do 
    if [ -d "$infile" ]; then 
    readdir "$infile" 
    else 
    renamer "$infile" 
    fi 
done 

ls 

我想創建一個bash腳本來遞歸通過一個目錄和重命名文件,以刪除空格,破折號和其他字符。我已經得到了我所需要的腳本,除了它的遞歸部分。我仍然對此感到陌生,所以它不像應該那樣高效,但它很有效。任何人都知道如何做這個遞歸?

#/bin/bash 
for infile in *.*; 
do 
#replace " - " with a single underscore. 
NEWFILE1=`echo $infile | sed 's/\s-\s/_/g'`; 
#replace spaces with underscores 
NEWFILE2=`echo $NEWFILE1 | sed 's/\s/_/g'`; 
#replace "-" dashes with underscores. 
NEWFILE3=`echo $NEWFILE2 | sed 's/-/_/g'`; 
#remove exclamation points 
NEWFILE4=`echo $NEWFILE3 | sed 's/!//g'`; 
#remove commas 
NEWFILE5=`echo $NEWFILE4 | sed 's/,//g'`; 
mv "$infile" "$NEWFILE5"; 
done; 
+0

我覺得它比計算器更多的超級用戶,但如果它只是爲了解決一個特定的Linux操作系統中的東西我會使用查找和重命名。 – Fredrik 2010-11-22 20:49:49

+0

如果您使用`*。*`,則不匹配不包含「`。」「的文件。在Bash中,僅僅使用`*` – 2010-11-22 21:27:56

回答

2

所有這些分號是多餘的,沒有理由使用所有這些變量。如果您想要將sed命令放在單獨的行中並散佈詳細的註釋,您仍然可以執行此操作。

#/bin/bash 
find . | while read -r file 
do 
    newfile=$(echo "$file" | sed ' 
     #replace " - " with a single underscore. 
     s/\s-\s/_/g 
     #replace spaces with underscores 
     s/\s/_/g 
     #replace "-" dashes with underscores. 
     s/-/_/g 
     #remove exclamation points 
     s/!//g 
     #remove commas 
     s/,//g') 
    mv "$infile" "$newfile" 
done 

這是更短:

#/bin/bash 
find . | while read -r file 
do 
    # replace " - " or space or dash with underscores 
    # remove exclamation points and commas 
    newfile=$(echo "$file" | sed 's/\s-\s/_/g; s/\s/_/g; s/-/_/g; s/!//g; s/,//g') 
    mv "$infile" "$newfile" 
done 

時間更短:

#/bin/bash 
find . | while read -r file 
do 
    # replace " - " or space or dash with underscores 
    # remove exclamation points and commas 
    newfile=$(echo "$file" | sed 's/\s-\s/_/g; s/[-\s]/_/g; s/[!,]//g') 
    mv "$infile" "$newfile" 
done 
0

在bash 4中,設置globstar選項允許遞歸通配。

shopt -s globstar 
for infile in ** 
... 

否則,請使用find

while read infile 
do 
... 
done < <(find ...) 

find ... -exec ... 
+0

更加地道,好吧,看起來我還有很多東西需要學習,因爲你現在讓我感到困惑。我實際上是在Windows上使用bash,使用gnuwin32工具和win-bash,這顯然是基於bash 1.14.2。 到目前爲止,感謝您的幫助,我會嘗試看看能否從這裏弄清楚,但如果您可以完成腳本以供我查看爲例,則將不勝感激。 – Ryan 2010-11-22 20:53:03

3

find是能夠顯示在一個文件系統層次的所有元素的命令。您可以使用它來在每個找到的文件上執行命令,或者將結果傳送到將處理執行部分的xargs

請注意,for infile in *.*不適用於包含空格的文件。檢查-print0選項find,加上-0選項xargs

0

我以前用'find'來定位文件,然後讓它執行另一個應用程序。 見'-exec'

0
rename 's/pattern/replacement/' glob_pattern 
+0

例如`重命名's/\ s + // g''文件名與Whitespace'`將它重命名爲'FileNameWithWhitespace' – 2010-11-22 21:30:32