2012-03-07 111 views
1

我想使用shell腳本列出所有目錄。我使用下面的代碼:列出名稱中包含空格的所有目錄

DIR="$1" 

if [ $# -ne 1 ] 
then 
    echo "Usage: $0 {dir-name}" 
exit 1 
fi 

cd "$DIR" 
SAVEIFS=$IFS 
IFS=$(echo -en "\n\b") 
for user in $(ls -d */) 
do 
    for dirs in $(ls -d $PWD/$user*) 
    do 
    echo $PWD/$user/$dirs; 
    done 
done 
IFS=$SAVEIFS 

這是爲我工作,如果該目錄沒有關於它的任何空間,否則拆分在它的每一個空間的輸出。我得到了以下的輸出:

[email protected]:~/software$ sh test.sh /media/2C44138344134F48/RB1 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/3001/*: No such file or directory 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/3002/*: No such file or directory 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory 
ls: cannot access /m: No such file or directory 
ls: cannot access dia/2C44138344134F48/RB1/3003/*: No such file or directory 
+0

你可以更精確的問題:你只需要帶有空格的目錄嗎?或者你是否需要在所有目錄上「做你的東西」,包括名稱中包含空格的目錄? – JScoobyCed 2012-03-07 06:26:40

+0

您需要使用BASH運行腳本「bash test.sh/media/2C44138344134F48/RB1」 – JScoobyCed 2012-03-07 06:40:24

+0

我需要列出所有目錄,包含或不包含空格。 – biztiger 2012-03-07 06:42:44

回答

2

編輯:如果您使用bash外殼去掉ls優化腳本(從tripleee

#!/bin/bash 

SAVEIFS=$IFS 
IFS=$(echo -en "\n\b") 
DIR="$1" 

if [ $# -ne 1 ] 
then 
echo "Usage: $0 {dir-name}" 
exit 1 
fi 

cd "$DIR" 

for user in $(ls -d */) 
do 
for dirs in $(ls -d $user*) 
    do 
    echo $dirs 
done 
done 

IFS=$SAVEIFS 

運行上面的 「test.sh」這樣的腳本:

bash test.sh /media/2C44138344134F48/RB1 

或簡單地(如果你已經在BASH和您已設置的可執行標誌)

./test.sh /media/2C44138344134F48/RB1 

Reference

+0

現在它不能列出任何目錄。我附上我原來的帖子輸出.. – biztiger 2012-03-07 06:25:34

+0

奇怪。它適用於我的Ubuntu,使用BASH shell。我錯過了幾分鐘前的腳本的最後一行(將原始IFS放回去的那一行),也許你需要嘗試一個新的shell(只需鍵入'bash') – JScoobyCed 2012-03-07 06:31:12

+0

我也在使用ubuntu 11.10和bash shell但我仍然收到錯誤 – biztiger 2012-03-07 06:40:42

1

這實際上是最容易使用的發現:

find PATH -type d -name '* *' 

或者,如果你需要做的事情與每一個結果,認爲管道傳輸到xargs

find PATH -type d -name '* *' -print0 | xargs -0 run-some-command 

或者如果你只是想要所有的目錄安全es披着披肩成參數,那麼:

find PATH -type d -print0 | xargs -0 run-some-command 

裏面run-some-command,每個參數的腳本將被正確設置到每個目錄名稱,無論它所包含的字符。

+0

同意,也許OP問題與帖子內容不符。我想他想處理一個目錄中的所有目錄,但是他的腳本因爲名稱中有空格而失敗。 – JScoobyCed 2012-03-07 06:25:44

+0

是的,我需要在其中處理al目錄。 – biztiger 2012-03-07 06:44:21

0

下面的簡單腳本將做你所需要的。

#!/bin/bash 

function usage() { 
     echo "Usage: `basename $0` <dir-name>" 
} 


if [ "x$1" = "x" ] 
then 
     usage 
     exit 0 
fi 

find "$1" -type 'd' 
相關問題