Reputation: 2115
I need to exclude multiple directories from find result. I know how to do it by hard coding in the find
command, but can it be possible to store the list of excluded dir as a variable or array and later used in find command ?
Here is my working command: In this command, I am touching
a file called dumped
inside all the directories in the current directories with maxdepth 1
except .
, ./tmp
, ./garbage
directories.
find . -maxdepth 1 ! -path "." ! -path "./tmp" ! -path "./garbage" -type d -exec touch {}/dumped 2>/dev/null \;
If you notice I have provided hard coding in the find
command for the directories to be excluded.
If there any way to store the list in a variable and pass to the find
command ?
something like exclude_list=".|./tmp|./garbage"
and use it afterwards ?
I tried following but it did not worked:
find . -maxdepth 1 ! -path ${exclude_list}
Upvotes: 0
Views: 385
Reputation: 246744
Untested: use an array
find_clauses=(
-maxdepth 1
! -path "."
! -path "./tmp"
! -path "./garbage"
-type d
-exec touch {}/dumped ';'
)
find . "${find_clauses[@]}" 2>/dev/null
If you want to put the excluded directories in a list, you can still build the find clauses dynamically:
exclude_dirs=( . ./tmp ./garbage )
find_clauses=( -maxdepth 1 )
for d in "${exclude_dirs[@]}"; do find_clauses+=( ! -path "$d" ); done
find_clauses+=(
-type d
-exec touch {}/dumped ';'
)
find . "${find_clauses[@]}" 2>/dev/null
Upvotes: 2