了解find中的转义括号

问题描述 投票:2回答:1

我将下面的内容拼凑在一起,并且似乎可以正常工作,但可能会出现“!-empty”例外。我正在学习的一件事(就我而言)只是因为某些东西有效,并不意味着它是正确的或正确形成的...我的问题是如何确定需要括号的内容以及查找中没有的内容命令?

在OS X中,-并且“由两个表达式的并置表示,不必指定”

我的目标是找到:查找超过5分钟的旧目录,不是空目录,也不是.dot(隐藏的-即“。”和“ ..”)]

count="$( find . -type d -mmin +5 \! -empty \( ! -iname ".*" \) | wc -l )"
echo $count
if [ "$count" -gt 0 ] ; then
    echo $(date +"%r") "$cust_name loc 9: "${count}" directories exist to process, moving them" >> $logFILE
    find . -type d -mmin +5 \! -empty \( ! -iname ".*" \) | xargs -I % mv % ../02_processing/

    cd $processingPATH

    # append the time and the directories to be processed in files_sent.txt
    date +"---- %a-%b-%d %H:%M ----" >> $filesSENTlog
    ls >> $filesSENTlog

    find . -type f -print0 | xargs -0 /usr/local/dcm4che-2.0.28/bin/dcmsnd $aet@$peer:$port
    echo $(date +"%r") "$cust_name loc 10: Processing ${count} items..." >> $logFILE

    # clean up processed studies > from processing to processed
    echo $(date +"%r") "$cust_name loc 11: Moving ${count} items to 03_processed" >> $logFILE
    mv * $processedPATH
else
    echo $(date +"%r") "$cust_name loc 12: there are no directories to process" >> $logFILE
fi

我可以做:

find . -type d -mmin +5 \! -empty \! -iname ".*"

?还是由于某些原因不正确?

bash shell find quoting
1个回答
11
投票

[find具有按优先顺序排列的以下运算符(最高->最低)

  1. ()
  2. !|-not
  3. -a|-and
  4. -o|-or
  5. [,(仅GNU

注意:所有testsactions都有一个隐含的-a相互链接

因此,如果您不使用任何运算符,则不必担心优先级。如果像您一样使用not,您也不必担心优先级,因为! exp exp2的优先级高于隐含的优先级,因为(! exp) AND (exp2)将按预期被视为!and

优先级高的示例

> mkdir empty && cd empty && touch a && mkdir b
> find -mindepth 1 -type f -name 'a' -or -name 'b'
./a
./b

以上被视为find -mindepth 1 (-type f AND -name 'a') OR (-name 'b')

> find -mindepth 1 -type f \( -name 'a' -or -name 'b' \)
./a

以上被视为find -mindepth 1 (-type f) AND ( -name 'a' OR -name 'b')

注意:选项(即-mindepth,-noleaf等)始终为true

结论

find的以下两种用法完全相同

  • find . -type d -mmin +5 \! -empty \( ! -iname ".*" \) | wc -l
  • find . -type d -mmin +5 \! -empty \! -iname ".*" | wc -l

两者均被视为

  • find . (-type d) AND (-mmin +5) AND (! -empty) AND (! -iname ".*") | wc -l
© www.soinside.com 2019 - 2024. All rights reserved.