I am building a find command programatically:
find "${SRC_DIR}" "${FILE_NAMES}" -type f -mtime +14 -level 0
Where FILE_NAMES is the file patterns that I need to match, which is a simple whitespace delimited list of patterns (*.txt, *.html, etc).
To format this list correctly for the find command, it can’t just be a list though, it has to look like:
-name *.txt -o -name *.html
My solution was to use:
FILE_NAMES=$(echo ${FILES} | sed -e 's/\s+/-o -name /g')
But it’s not working, specifically it doesn’t seem to understand that I want to replace whitespace. Does anyone have a better way to do this?
Not all seds support perl-like place holders for space, digits, etc. I don’t have a way to test your exact case, but the following should be a good guide.
Also, note that
find -predicate -predicate ....are ANDed by default. to make the-owork, you have to surrond the whole thing in( (parens) )AND so the shell won’t see the paren as a request to launch a subshell, you have to escape the parens, i.e\( .... -o .... \). So that leads us toOUTPUT
which just indicates that my find doesn’t support -level either.
edit removed extra
"chars surounding the\(....\).also
If you’re using a ksh93, it maybe possible to use variable modifiers, like
But you’ll need to either just concatenate the opening
-nameor do a 2nd substitution on the saved value of FILE_NAME.You can see if you have ksh93 by issuing the following cmd
If you get a value, then you have ksh93.
IHTH