0

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?

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

1 Answer 1

0

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'
findexcl=
set -f
for p in $excludes
do
  findexcl="$findexcl ! -path $p"
done
echo find ... $findexcl ...

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.