Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

Linux Find Exclude Multiple Directories

find . -type d \( -name media -o -name images -o -name backups \) -prune -o -print

find . - Find all files in this directory (.)

-type d - directory or folder

-prune - ignore the proceding path of ...

\( -name media -o -name images -o -name backups \) - The -o simply means OR

-o -print - Then if no match print the results, (prune the directories and print the remaining results)

As always in Linux there are many ways to achieve the same result, and the patient may prefer to build up the find command using the path attribute. Please note that you may need to specify the path with a prefix of "./" and no trailing slash.

find . -path './media' -prune -o -path './images' -prune -o -path './backups' -prune -o -print

An important difference here is that the first command will prune ANY directory in the path that matches the name, such as './media' and './public/media', where as the second format will only prune the explict path './media'.

Share this!