2016-08-04 30 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