Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find -name with multiple filenames using shell variable

Tags:

find

bash

shell

I have a find command that finds files with name matching multiple patterns mentioned against the -name parameter

find -L . \( -name "SystemOut*.log" -o -name "*.out" -o -name "*.log" -o -name "javacore*.*" \)

This finds required files successfully at the command line. What I am looking for is to use this command in a shell script and join this with a tar command to create a tar of all log files. So, in a script I do the following:

LIST="-name \"SystemOut*.log\" -o -name \"*.out\" -o -name \"*.log\" -o -name \"javacore*.*\" "
find -L . \( ${LIST} \)

This does not print files that I am looking for.

First - why this script is not functioning like the command? Once it does, can I club it with cpio or similar to create a tar in one shot?

like image 456
ring bearer Avatar asked Dec 27 '22 01:12

ring bearer


2 Answers

Looks like find fails to match * in patterns from unquoted variables. This syntax works for me (using bash arrays):

LIST=( -name \*.tar.gz )
find . "${LIST[@]}"

Your example would become the following:

LIST=( -name SystemOut\*.log -o -name \*.out -o -name \*.log -o -name javacore\*.\* )
find -L . \( "${LIST[@]}" \)
like image 149
lunixbochs Avatar answered Jan 25 '23 21:01

lunixbochs


eval "find -L . \( ${LIST} \)"
like image 41
h0tw1r3 Avatar answered Jan 25 '23 23:01

h0tw1r3