0

The -prune option does not seem to work for me this way on MacOS Monterey (12.01):

find ~/Documents -path "~/Documents/_learning/*" -prune -o -type f -print

It does not exclude the files in ~/Documents/_learning directory.

What am I doing wrong? (The command was constructed using e.g. this answer, as well as several others.)

Here is hopefully a reproducible experiment. (You can pick any two nested directories available to you with one or more files in them and do a similar experiment.) Counting the files in ~/Documents:

find ~/Documents -type f -print | wc -l
     428

find ~/Documents/_learning -type f -print | wc -l
     279

... then excluding the ~/Documents/_learning directory should produce 149 files - yet:

find ~/Documents -path "~/Documents/_learning/*" -prune -o -type f -print | wc -l
     428

... i.e. the same number as without the exclusion. Using the full path (/Users/<userID>/Documents/...) - does work. Using a relative path with ./Documents/... - does not. How I know:

find ~/Documents -type f | grep -v "/Documents/_learning/" | wc -l
     149

Which brings me to: what am I doing wrong? Why does only the full path work?

Thank you!

P.S. -not -path ... - same behavior;

find ~/Documents -not -path "~/Documents/_learning/*" -type f -print | wc -l
     428
find ~/Documents -type f -not -path "~/Documents/_learning/*" | wc -l
     428
find ~/Documents -type f -not -path ~/Documents/_learning/ | wc -l
     428

Yet it does work with a full path:

find ~/Documents -type f -not -path "/Users/<userID>/Documents/_learning/*" | wc -l
     149
kindzmarauli
  • 175
  • 8
  • 1
    The shell doesn't expand `~` if it's in quotes. Try `... -path ~/"Documents/_learning/*" ...`. Note that the `/` must also be outside the quotes, but the `*` needs to be inside the quotes 9or escaped) to prevent *it* from expanding, so `~/Documents/_learning/"*"` and `~/Documents/_learning/\*` should also work. – Gordon Davisson Jan 02 '22 at 02:15
  • that worked @GordonDavisson (`find ~/Documents -path ~/Documents/_learning/\* -prune -o -type f -print | wc -l`) - thank you! Make it an answer? – kindzmarauli Jan 02 '22 at 02:42
  • There's another question about the same problem (in slightly different context) that already has a good answer, so it's better to link it to that one. – Gordon Davisson Jan 02 '22 at 02:55
  • Just removing the double quotes doesn't do it though - so to me, the questions (and answers) - while somewhat related - are not the same. It also ties in to `find`'s counterintuitive inability to exclude a path w/o a wildcard (that subsequently needs to be escaped or enclosed in quotes). I.e. in my eyes, ppl using `find` might benefit from an explicitly stated answer based on your first comment. – kindzmarauli Jan 02 '22 at 03:04

0 Answers0