Search code examples
linuxshellfind

Exclude range of directories in find command


I have directory called test which has sub folders in the date range like 01,02,...31. This all sub folders contain .bz2 files in it. I need to search all the files with .bz2 extension using find command but excluding particular range of directories. I know about find . -name ".bz2" -not -path "./01/*", but writing -not -path "./01/*" would be so pathetic if I would want to skip 10 directories. So how would I skip 01..19 subdirectories in my find command ?


Solution

  • You can use wildcards in the pattern for the option -not -path:

    find ./ -type f -name "*.bz2" -not -path "./0*/*" -not -path "./1*/*
    

    this will exclude all directories starting with 0 or 1. Or even better:

    find ./ -type f -name "*.bz2" -not -path "./[01]*/*"