Jubayer ibna kalam Jubayer ibna kalam - 3 months ago 14
Bash Question

find command not working in directories with space and special character

I am trying to use "find" command for to find specific file name . However, it contains space and "()" symbol. I tried following

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


But it shows error like below :

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*


I also tried following :

find "{$cspath}" -print0


but the error happend again (this time printed \n in every space)

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


please suggest

Answer

The problem is cygpath -u $test generates multiple paths on new lines if test has whitespace - the fix is cygpath -u "$test"

This works for me - cygwin tools in general can handle whitespace in paths - some care is still required - my rule is to never have whitespace in any path or filename if I can help it

$ 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)