-1

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

mike.dld
  • 2,929
  • 20
  • 21
  • There are extra or misplaced curly braces in your second attempt, should be `find "$cspath" -print0` . What says `ls -d "$cspath"`? – jlliagre Aug 21 '16 at 09:16
  • ls: cannot access '/cygdrive/e/dir/dir1/OneDrive'$'\n''-'$'\n''The'$'\n''Asia'$'\n''Group/TAG'$'\n''(1)/Works'$'\n''in'$'\n''Progress/Brian/Jaycees/Firecracker'$'\n''4'$'\n''Mile'$'\n''Road'$'\n''Race'$'\n''-'$'\n''2015'$'\n''Shirt_files': No such file or directory . one more thing, I am using cygwin – Jubayer ibna kalam Aug 21 '16 at 09:29
  • What about `echo "$cspath"`? – jlliagre Aug 21 '16 at 09:41
  • /cygdrive/e/dir/dir1/OneDrive - The Asia Group/TAG (1)/Works in Progress/Brian/Jaycees/Firecracker 4 Mile Road Race - 2015 Shirt_files # break through every space – Jubayer ibna kalam Aug 21 '16 at 10:44
  • There is no issue with `find`. The issue is you wrongly state in your question the `cspath` variable content is static while you actually build it programmatically. This variable contains embedded new lines that shouldn't be there. There is no way to figure out why without knowing how you affect it. – jlliagre Aug 21 '16 at 13:12
  • #!/bin/bash echo -n "Enter checking Path > " read -r spath echo $spath |sed "s*\\\*/*g" >spath test=`cat spath` cspath=`cygpath -u $test` echo $cspath . eco $cspath is showing perfect result . but it is breaking into lines in find command – Jubayer ibna kalam Aug 21 '16 at 13:33
  • That's because you don't quote it properly. `echo $variable` without quotes doesn't reliably reveal what's actually in `variable`. Read the linked duplicate. – tripleee Aug 21 '16 at 14:08

2 Answers2

1

You were close, the variables are written as $variable or ${variable} in bash.

Now you need quoting so that bash does not do field splitting and pathname expansion, this is important for you as you have spaces in path. Do:

find "${cspath}" -print0

or

find "$cspath" -print0

Your first try without quotes was failing because you have space(s) in file path and bash was performing field splitting on whitespaces, just quoting would do.

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • nope, the same error: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 – Jubayer ibna kalam Aug 21 '16 at 08:20
1

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)
  • I am invoking the variable $cspath from another output. its not static like your example – Jubayer ibna kalam Aug 21 '16 at 12:09
  • That might be the problem - show how cspath is actually set - also add echo "cspath \"$cspath\"" – pakistanprogrammerclub Aug 21 '16 at 12:34
  • #!/bin/bash echo -n "Enter checking Path > " read -r spath echo $spath |sed "s*\\\*/*g" >spath test=`cat spath` cspath=`cygpath -u $test` echo $cspath echo $cspath is always showing ok. but whenever i try to invoke this into find command, it is showing wrong output. also tried echo "cspath \"$cspath\"" but it breaks the space into new line cspath "/cygdrive/e/dir/dir1/OneDrive - The Asia Group/TAG (1)/Works in Progress/Brian/Jaycees/Firecracker 4 Mile Road Race - 2015 Shirt_files" – Jubayer ibna kalam Aug 21 '16 at 12:45
  • echo is not breaking anything - that's why I have echo print the escaped quotes - the newline is in cspath – pakistanprogrammerclub Aug 21 '16 at 12:56
  • It seems unnecessary to write the input path to a file then read the file into a variable then convert with cygpath unless you have tried to simulate the complex process through which the path is generated - cygpath has a number of options that let you convert from one kind of path to another - it should be possible to simply run cygpath directly on the input path – pakistanprogrammerclub Aug 21 '16 at 13:21
  • I checked but didnt find any new line in cspath – Jubayer ibna kalam Aug 21 '16 at 13:32
  • thanks a ton, man . It is working – Jubayer ibna kalam Aug 21 '16 at 16:02