find命令在具有空格和特殊字符的目录中不起作用

时间:2016-08-21 07:19:40

标签: bash shell

我正在尝试使用"找到"用于查找特定文件名的命令。但是,它包含空格和"()"符号。我试过跟着

cspath="/cygdrive/e/dir/dir1/OneDrive - The Asia Group/TAG (1)/Works in Progress/Brian/Jaycees/Firecracker 4 Mile Road Race - 2015 Shirt_files"

/usr/bin/find $cspath -print0

但它显示如下错误:

find: ‘/cygdrive/e/dir/dir1/OneDrive’: No such file or directory
find: ‘-’: No such file or directory
find: ‘The’: No such file or directory
find: ‘Asia’: No such file or directory
find: ‘Group/TAG’: No such file or directory
find: ‘(1)/Works’: No such file or directory
find: ‘in’: No such file or directory
find: ‘Progress/Brian/Jaycees/Firecracker’: No such file or directory
find: ‘4’: No such file or directory
find: ‘Mile’: No such file or directory
find: ‘Road’: No such file or directory
find: ‘Race’: No such file or directory
find: ‘-’: No such file or directory
find: ‘2015’: No such file or directory
find: ‘Shirt_files’: No such file or directory*

我也尝试过:

find "{$cspath}" -print0

但是错误再次发生(这次在每个空间都打印出来)

find: ‘{/cygdrive/e/dir/dir1/OneDrive\n-\nThe\nAsia\nGroup/TAG\n(1)/Works\nin\nProgress/Brian/Jaycees/Firecracker\n4\nMile\nRoad\nRace\n-\n2015\nShirt_files}’: No such file or directory

请建议

2 个答案:

答案 0 :(得分:1)

您很接近,变量在$variable中写为${variable}bash

现在您需要引用以便bash不进行字段拆分和路径名扩展,这对您来说很重要,因为路径中有空格。做:

find "${cspath}" -print0

find "$cspath" -print0

你没有引号的第一次尝试失败了,因为你在文件路径中有空格而且bash正在空格上执行字段拆分,只是引用会这样做。

答案 1 :(得分:0)

问题是cygpath -u $test如果测试有空格,则在新行上生成多个路径 - 修正为cygpath -u "$test"

这对我有用 - 一般来说,cygwin工具可以处理路径中的空白 - 仍需要一些小心 - 我的规则是如果我可以帮助它,就不会在任何路径或文件名中留下空格

$ pth="/cygdrive/c/dir with spaces (and parens)"
$ find "$pth"
/cygdrive/c/dir with spaces (and parens)
/cygdrive/c/dir with spaces (and parens)/file0 with spaces (and parens)
/cygdrive/c/dir with spaces (and parens)/file1 with spaces (and parens)

# cygwin and find versions
$ uname -r
2.5.1(0.297/5/3)
$ find --version
find (GNU findutils) 4.6.0
Packaged by Cygwin (4.6.0-1)