简体   繁体   中英

linux find command is not taking proper argument

find . -maxdepth 1 ! -path . -type f ! -name "*.gz" ${FILE_PATTERN} -mtime +${DAYS_AFTER_ARCHIVE} 

I am trying to execute above command inside the script where ${FILE_PATTERN} and ${DAYS_AFTER_ARCHIVE} are the variable provided while executing the script. The variable value for ${FILE_PATTERN} would be ! -name "*warnings*" ! -name "*warnings*" . I am looking for executing above command as like below command in script

find . -maxdepth 1 ! -path . -type d ! -name "*warnings*" -mtime +7

I am providing file pattern argument as "! -name " warnings "" but receiving following error message

find: paths must precede expression 
Usage: find [-H] [-L] [-P] [path...] [expression]

suggest on above.

First of all

-name "*.gz" ${FILE_PATTERN}

has too many option values (this is what usually causes the message shown)

If you use bash or similar, escape the exclamation marks

find . -maxdepth 1 \! -path . -type f \! -name "*.gz" ${FILE_PATTERN} -mtime +${DAYS_AFTER_ARCHIVE} 

I am providing file pattern argument as "! -name "warnings"" but receiving following error message

You can't combine flags and their values like that. Also, you can't nest " like that. So, it could be like

 ! -name "$FILE_PATTERN"

If you're using BASH you can make use of BASH arrays:

# find options
FILE_PATTERN=(! -name "warnings*")

# build the find command
cmd=(find . -maxdepth 1 ! -path . -type f \( ! -name "*.gz" "${FILE_PATTERN[@}}" \) -mtime +${DAYS_AFTER_ARCHIVE})

# execute the command
"${cmd[@]}"

If not using BASH then you will have to use eval with caution :

FILE_PATTERN='! -name "warnings*"'
eval find . -maxdepth 1 ! -path . -type f ! -name "*.gz" "${FILE_PATTERN}" -mtime +${DAYS_AFTER_ARCHIVE} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM