2

There is some strange behavior while excluding path from find:

find ~ -not -path "~/sandboxes/*" -name 'some-file.vmdk'

gives:

/home/user/VMs/win/some-file.vmdk
find: ‘/home/user/sandboxes/debian7.amd64.buildd/root/...’: Permission denied
find: ‘/home/user/sandboxes/debian7.amd64.buildd/var/...’: Permission denied

What's wrong?

P.S. unfortunately -prune doesn't work too:

find ~ -path "/home/user/sandboxes/*" -prune -o -name 'some-file.vmdk'

gives more weird results:

/home/user/nemu_vm/win/some-file.vmdk
/home/user/sandboxes/debian7.amd64.buildd
/home/user/sandboxes/debian9.amd64.buildd

Useful link

  • Could you replace -path "~/sandboxes/*" with -path "$HOME/sandboxes/*" – Inian Apr 9 at 9:55
  • @Inian there is no effect. The same output – z0lupka Apr 9 at 10:01
  • Can you confirm you are you looking for regular files named some-file.vmdk only? It may be useful to state explicitly what the expected output is, we may wonder if you want the path /home/user/sandboxes (not its content) to be printed or not. – fra-san Apr 9 at 12:35
  • @fra-san Yep, it's about regular files it this case. I want find not to even enter /home/user/sandboxes – z0lupka Apr 9 at 13:29
2

Your command

find ~ -path "/home/user/sandboxes/*" -prune -o -name 'some-file.vmdk'

prints

/home/user/sandboxes/debian7.amd64.buildd
/home/user/sandboxes/debian9.amd64.buildd

because the default action when no action is supplied is to output the found pathnames. The above pathnames are found, and then those paths are pruned. Pruning a search path does not exclude these pathnames from being printed.

However, if you add -print to the very end, as in

find "$HOME" -path "$HOME/sandboxes" -prune -o -name 'some-file.vmdk' -print

then those pathnames would not be printed. This is because now you have an explicit action (the -print), so no default actions are triggered. The -print only applies to the right hand side of -o.

Note that the * is not needed, and that the variable $HOME is easier to work with than ~, especially in scripts.


Your first command,

find ~ -not -path "~/sandboxes/*" -name 'some-file.vmdk'

very likely does not work as ~ is not expanded within quotes.

Assuming you used $HOME instead, it also does not prune the search path, which means it would still enter ~/sandboxes, but it would never print any pathnames from beneath that path. Since it enters the directory, it would still give you the permission errors when it reaches the inaccessibly directories.

  • Thanks for clarifying! BTW find "$HOME" -path "$HOME/sandboxes" -prune -type f -o -name 'some-file.vmdk' helped too as @fra-san suggested – z0lupka Apr 9 at 13:23
  • 1
    @z0lupka Please note that what I wrote in my previous comment is a bit ugly. Semantically, the correct form would be find "$HOME" -path "$HOME/sandboxes" -prune -false -o -name 'some-file.vmdk', where false is used to prevent any action when prune succeeds (sorry for this invasion, Kusalananda). – fra-san Apr 9 at 13:34
0

You can try this way :

find ~ \! -path "*/sandboxes/*" -name 'some-file.vmdk'

Or with -prune :

find ~ -path "*/sandboxes" -prune -o -name 'some-file.vmdk'

but can't find a way to remove the print of ~/sandboxes

  • unfortunately didn't help – z0lupka Apr 9 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.