Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

String variable as part of expression in find command

$
0
0
Hi, I am new in scripting, and I am currently working on a script that will look for other files in a certain directory and exclude some file type.

this works fine:
Code:

Find_File2Exclude=`find ${paths[$k]} -maxdepth 1 -type f \( ! -iname '*.out' ! -iname '*.auc' ! -iname '*.cps' ! -iname '*.log' ! -iname '*.dat' \) -printf '%t %p\n' | sort -k 1 -n | head -n 1 | cut -d'/' -f3`
echo "other file searched == $Find_File2Exclude"

but the excluded file to search [eg. *.out, *.dat] change from time to time so I have to change it into a variable. However, if I change my code into this..
Code:

exclude=" ! -iname '*.out' ! -iname '*.log' ! -iname '*.cps' ! -iname '*.auc' ! -iname '*.dat'"
Find_File2Exclude=`find ${paths[$k]} -maxdepth 1 -type f \( $exclude \) -printf '%t %p\n' | sort -k 1 -n | head -n 1 | cut -d'/' -f3`
echo "$Find_File2Exclude"

it will give me this error:
Quote:

find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
I realize that the "! -iname" expression shouldn't be inside the string variable $exclude, but I need to concatenate other file to be exclude in the future, how can I make it work? Or is it still possible?

Thanks in advance.

Viewing all articles
Browse latest Browse all 16232

Trending Articles