![](/img/trans.png)
[英]On Linux, FIND command with LS not showing proper result without option -d
[英]linux find command is not taking proper argument
find . -maxdepth 1 ! -path . -type f ! -name "*.gz" ${FILE_PATTERN} -mtime +${DAYS_AFTER_ARCHIVE}
我試圖在腳本中執行以上命令,其中${FILE_PATTERN}
和${DAYS_AFTER_ARCHIVE}
是執行腳本時提供的變量。 ${FILE_PATTERN}
的變量值為! -name "*warnings*"
! -name "*warnings*"
。 我正在尋找像腳本中的以下命令一樣執行上述命令
find . -maxdepth 1 ! -path . -type d ! -name "*warnings*" -mtime +7
我提供文件模式參數為“!-name” warnings “”,但收到以下錯誤消息
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
在上面建議。
首先
-name "*.gz" ${FILE_PATTERN}
選項值太多(通常會導致顯示此消息)
如果您使用bash或類似工具,請避免出現感嘆號
find . -maxdepth 1 \! -path . -type f \! -name "*.gz" ${FILE_PATTERN} -mtime +${DAYS_AFTER_ARCHIVE}
我提供的文件模式參數為“!-name“ warnings””,但收到以下錯誤消息
您不能像這樣組合標志及其值。 另外,您不能像這樣嵌套"
! -name "$FILE_PATTERN"
如果您正在使用BASH,則可以使用BASH數組:
# 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[@]}"
如果不使用BASH,則必須謹慎使用eval
:
FILE_PATTERN='! -name "warnings*"'
eval find . -maxdepth 1 ! -path . -type f ! -name "*.gz" "${FILE_PATTERN}" -mtime +${DAYS_AFTER_ARCHIVE}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.