I have a folder ghostbusters/ and I’m working in that folder.
ghostbusters$ ls -Alh
egon/
peter/
ray/
winston/
I want to get a listing of all files within ghostbusters/ except for those within peter/. The list must consist of each file or folder’s full path with respect to the execution directory. Based on this answer, I try the following command:
$ sudo find -mindepth 1 -path /the/real/ghostbusters/peter -prune -o -printf '%Pn'
To double-check that nothing in peter/ is being returned, I pipe this to grep to look for filenames starting with peter/. It’s a good thing I did, because in the grand Linux tradition of spending days at a time struggling against the very tools that are supposed to help you, I find lots of files from peter/.
ghostbusters$ sudo find -mindepth 1 -path /the/real/ghostbusters/peter -prune -o -printf '%Pn' | sort | grep "peter/" | head -10
peter/proton_pack.jpg
peter/slimer.pdf
peter/witty_zingers/
peter/witty_zingers/its_true_officer.txt
peter/witty_zingers/you_say_yes.txt
...
It is clear that this approach to excluding peter/ from the find command does not work.
Another answer on the same page linked above suggests a construction using -not -path, but the consensus is that this is inefficient because it still accesses all of the ignored directories. I don’t want that, because the witty_zingers/ subdirectory is quite large, so this will slow down the script.
How do I exclude a directory from the find command?