了解find中的转义括号 [英] Understanding escaped parentheses in find

查看:225
本文介绍了了解find中的转义括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've cobbled together what is below, and it seems to work, with the possible exception of the "! -empty". One thing I'm learning (as I go) is that just because something works, doesn't mean it's right or formed correctly...The question I have is how do you determine what requires parentheses and what doesn't in a find command?

在OS X中,-和被两个表达式的并置表示,不必指定"

In OS X, -and is "implied by the juxtaposition of two expressions it does not have to be specified"

我的目标是找到: 查找超过5分钟的旧目录,而不是空目录,并且不是.dot(隐藏的-即."和"..")

My goal is to have find: find directories that are over 5 minutes old, not empty, and are not .dot (hidden -i.e. "." and "..")

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 ".*"

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

? or is that not correct for some reason?

推荐答案

find按优先顺序列出了以下运算符(最高->最低)

find has the following operators listed in order of precedence (highest -> lowest)

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

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

Note: All tests and actions have an implied -a linking each other

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

So if you are not using any operators, you don't have to worry about precedence. If you are just using not like in your case, you don't really have to worry about precedence either, since ! exp exp2 will get treated as (! exp) AND (exp2) as expected, due to ! having higher precedence than the implied 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等)始终为真

结论

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

The following two uses of find are exactly the same

  • find . -type d -mmin +5 \! -empty \( ! -iname ".*" \) | wc -l
  • find . -type d -mmin +5 \! -empty \! -iname ".*" | wc -l
  • 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

这篇关于了解find中的转义括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆