Motez Musa
Motez Musa

Reputation: 13

Excluding an array/list of directories in find

I'm trying to use an array supplied from a config file to exclude a list of directories from find. A simple example of the array looks like:

excludedDirList2[0]='*.'
excludedDirList2[1]='node_modules'

I've been messing around with the -prune and ! -path options but I can't figure out a way to dynamically read the array, generate the find, and make it actually work.

An example of a command that works but doesn't dynamically read the array

find $dir -type f -name "hidden.txt" ! -path "${excludedDirList[1]}" ! -path "${excludedDirList[0]}"

Upvotes: 1

Views: 1132

Answers (1)

KamilCuk
KamilCuk

Reputation: 140890

If it works, just add ! and -path in front of every element of the array and pass it to find.

 excludedDirList2=('*.' 'node_modules')
 findargs=()
 for i in "${excludedDirList2[@]}"; do
       findargs+=('!' '-path' "$i")
 done
 find "$dir" -type f -name "hidden.txt" "${findargs[@]}"

Upvotes: 6

Related Questions