Monday 9 July 2018

unix - Exclude folder using `find` command


I'm using the find command on a Mac to search for a folder called test1. Now test1 folder could be present in the .Trash folder also. How do I exclude the .Trash folder from getting reported in search results or basically any folder I wish to exclude?


$ find $HOME  -name test1 -type d
/Users/theuser/.Trash/test1
/Users/theuser/test1
/Users/theuser/Downloads/test1

I want the result to be just


/Users/theuser/test1
/Users/theuser/Downloads/test1

I used grep:


find $HOME  -name test1 -type d | grep -v '.Trash' 

to filter out the .Trash result, but I'm interested in knowing if using find alone achieves the same results.



Answer



find $HOME -path  $HOME/.Trash -prune -o -name test1 -type d -print

By explicitly using -print you avoid the spurious printing of .Trash.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...