2016-08-04 68 views
0

我必須在Linux中使用包含空格字符的路徑, 我的命令在非空間路徑中工作正常,但如果路徑包含空間。如何解決在Linux bash的'find'命令中的路徑包含空間

#!/bin/bash 
for i in $(find "." -maxdepth 1 -mindepth 1 -type d); do 
    b=$(echo "$i" | awk '{print substr($1,3); }') 
    echo "This file contain <Modified date> <ActualSize Byte> <FullPath>" > "$i"/"$b".txt 
    find "$i" -type f ! -iname '*thumbs.db*' -print0 | xargs -0 stat -c "%y %s %n" >> "$i"/"$b".txt 
done 

這是我的文件夾列表。

. 
./Folder 1 
./Folder 2 
./Folder 3 

腳本的錯誤如下。

./xyz.sh: line 4: ./Folder/Folder.txt: No such file or directory 
find: ./Folder: No such file or directory 
./xyz.sh: line 5: ./Folder/Folder.txt: No such file or directory 
./xyz.sh: line 4: 1/.txt: No such file or directory 
find: 1: No such file or directory 
./xyz.sh: line 5: 1/.txt: No such file or directory 
./xyz.sh: line 4: ./Folder/Folder.txt: No such file or directory 
find: ./Folder: No such file or directory 
./xyz.sh: line 5: ./Folder/Folder.txt: No such file or directory 
./xyz.sh: line 4: 2/.txt: No such file or directory 
find: 2: No such file or directory 
./xyz.sh: line 5: 2/.txt: No such file or directory 
./xyz.sh: line 4: ./Folder/Folder.txt: No such file or directory 
find: ./Folder: No such file or directory 
./xyz.sh: line 5: ./Folder/Folder.txt: No such file or directory 
./xyz.sh: line 4: 3/.txt: No such file or directory 
find: 3: No such file or directory 
./xyz.sh: line 5: 3/.txt: No such file or directory 
+0

你想對文件做什麼? – 123

回答

5

不要超過find命令for i in $(find . -type f)語法不循環,見Bash Pit-falls使用find-print0,爲下面一個while循環保存在文件夾/文件名的特殊字符: -

見什麼manfind頁說的有關-print0選項: -

-print0 
      True; print the full file name on the standard output, followed by 
      a null character (instead of the newline character that -print 
      uses). This allows file names that contain newlines or other 
      types of white space to be correctly interpreted by programs that 
      process the find output. This option corresponds to the -0 option 
      of xargs. 

使用在相同一個腳本如下。

#!/bin/bash 

while IFS= read -r -d '' folder; do 

    # Your script goes here 

done < <(find . -maxdepth 1 -mindepth 1 -type d -print0) 
+0

尊敬的Inian,我現在使用'while IFS = read -r -d''文件夾;做 echo $文件夾 找到「$ folder」-type f>「$ folder」/File.txt done <(find。-maxdepth 2 -mindepth 2 -type d -print0)'現在我只能輸出爲「 FILE.TXT」。我想使用深度2作爲文本文件的文件名,可以嗎? – NNOPP

+0

@NoppornPhantawee:請將它作爲單獨的問題發佈。謝謝! – Inian

+0

謝謝伊恩的回覆,我張貼在這裏新的步驟>> http://stackoverflow.com/questions/38780041/how-to-use-the-depth-level-2-or-maxdepth-as-output-text-文件換我-的bash腳本 – NNOPP

0

只是改變IFS非空間(在那裏發現:How to loop through file names returned by find? ):

IFS="\r\n" 

Alsol拉梅(但工作)只是增加空間字符一個sed過濾/不過濾

for ix in $(find "." -maxdepth 1 -mindepth 1 -type d | sed "s/ /%SPC%/g"); do 
    i=$(echo "$ix" | sed "s/%SPC%/ /g") 
    b=$(echo "$i" | awk '{print substr($1,3); }') 
    echo "This file contain <Modified date> <ActualSize Byte> <FullPath>" > "$i"/"$b".txt 
    find "$i" -type f ! -iname '*thumbs.db*' -print0 | xargs -0 stat -c "%y %s %n" >> "$i"/"$b".txt 
done