在名称中包含空格的文件夹中搜索

时间:2013-10-20 03:35:02

标签: bash

我已经尝试了下面的示例,但它没有返回任何内容,但是如果我删除了包含空格的名称,它就可以了。如何处理带有空格的foldernames?

MainDir="/Users/redres/Dropbox/Computer\ Ebooks/Skimmed"

# FS=$'\n'

while IFS= read -d $'\0' -r file ; do
            printf 'File found: '"'%s'"'\n' "$file"
    done < <(find "$MainDir" -iname '*' -print0)

i=1

for i in $(find $MainDir -type f); do echo "$i"; done;

感谢

2 个答案:

答案 0 :(得分:0)

MainDir="/Users/redres/Dropbox/Computer\ Ebooks/Skimmed"

引用空格或\ - 逃避它,而不是两者。使用

MainDir="/Users/redres/Dropbox/Computer Ebooks/Skimmed"

答案 1 :(得分:0)

代码的格式化足以令人哭泣!

但是,这应该有效,假设在空格之前目录名中实际上没有反斜杠:

MainDir="/Users/redres/Dropbox/Computer Ebooks/Skimmed"

while IFS= read -d '' -r file
do
    printf "File found: '%s'\n" "$file"
done < <(find "$MainDir" -print0)

例如:

$ mkdir -p 'Double  Spaced  Directory/Holograms/Object Lesson'
$ cp /dev/null 'Double  Spaced  Directory/Holograms/Object Lesson/Obstacle Course'
$ MainDir=$PWD/"Double  Spaced  Directory"
$ while IFS= read -d '' -r file
> do
>     printf "File found: '%s'\n" "$file"
> done < <(find "$MainDir" -iname '*' -print0)
File found: '/Users/jleffler/soq/Double  Spaced  Directory'
File found: '/Users/jleffler/soq/Double  Spaced  Directory/Holograms'
File found: '/Users/jleffler/soq/Double  Spaced  Directory/Holograms/Object Lesson'
File found: '/Users/jleffler/soq/Double  Spaced  Directory/Holograms/Object Lesson/Obstacle Course'
$