Load find pattern from a file?

Right now I have a bunch case statement wrapped around multiple groups of patterns. I want to make each pattern end-user definable in files instead. Can the bold parts be imported from a file simply? Code injection attack is not a big concern since there is access control on the share drive but if I have a choice in doing it in a relatively safe way, I'd prefer that.

find "$dataDir" \(    -iname 'redacted_1' \
                      -o -iname 'redacted_2' \
                      -o -iname 'redacted_3' \
                      -o -iname 'redacted_4' \) |
                           > "$scratchDir"file_list.txt

Mike

Depends, if there are always 4 values, and i assume its those redacted_{1..4} that would change, then yes its simple.
Handling an unknown is a little more complex.

pattern_file.txt

redacted_1 redacted_2 redacted_3 redacted_4
redacted_21 redacted_22 redacted_23 redacted_24
redacted_31 redacted_32 redacted_33 redacted_34

(untested)

while read r1 r2 r3 r4
do 	find "$dataDir" \
		( -iname '$r1' \
                  -o -iname '$r2' \
                  -o -iname '$r3' \
                  -o -iname '$r4' ) |
                  >> "$scratchDir"file_list.txt
done<pattern_file.txt

hth

No, it can be an arbitrary list of filters for find (not necessarily 4 and not necessarily -iname).

Mike

Wouldnt it then be easier to sum up all filters and then run either run one by one or all at once?

I'm not currently doing any iteration, only one find command. I don't particularly want iteration because that might cause duplicate entries. The multiline syntax \) ... )\ is just for human readability and does not change how find parses the filter.

Mike

Try (untested):

PATTERN=""
isFirst=true
while read pattern;do 
	$isFirst && \
		PATTERN+=" -iname \"$pattern\"" && \
		isFirst=false || \
		PATTERN+=" -o -iname \"$pattern\""
done<pattern_file.txt

find "$dataDir" $PATTERN >> "$scratchDir"file_list.txt

hth

The OP wants a program which will generate find statement(s).

For instance, an awk or shell program (or any other) could generate find statements.
But you need to define proper conditions for it.

Exact conditions like input file format, error handling desired (like possible timeouts,file locking, other processes on files in question etc.), exact find switches you wish you use in what scenario.

Just reading find switches from files does not bode well :smiley:

Can you post your entire code with a short explanation what you wish to change ?

2 Likes