I know how to exclude certain directories from find: find <path> ! -path <path to exclude>. But this can be too tedious to type out when I need to exclude a lot of directories or simply cannot be used when the list of directories I need to exclude are not known ahead of time.

So, say I have a list of directories stored in a variable. Can I use xargs to construct a find command that excludes each of these directories? If so, how?

  • are we talking a "basic" shell variable like v='file1 file2 file3' or something more complex, like a bash array?
    – Jeff Schaller
    Feb 16 '16 at 17:50
  • did the below answer your question?
    – Jeff Schaller
    Feb 20 '16 at 2:43

For the 'simple' variable case; note that the directories can't contain whitepace, as that's what separates them inside the variable.

excludes='dir1 dir2 dir3'
set -f
for p in $excludes
  findexcl="$findexcl ! -path $p"
echo find ... $findexcl ...

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.