Linux中find命令的prune参数探究

发布时间 2023-09-20 14:30:37作者: rancec

 

记得很久之前找过prune的参数使用,应急用了之后没有记录,但过了一段时间就会忘记,这次趁机找了一圈,包括Google-aosp里面的用法也对比参照了一下。

 

参考 https://www.jianshu.com/p/e0a9fb35601a

 

发现描述基本没问题,使用上还有些差异,特此记录一下:

<以下主要是  -prune -o -print 三者叠加情况的过滤实践 >

test-1:

date; mx=2; find . -maxdepth $mx -type d -path "./.git" -prune -o -path "./.repo" -prune -o -path "./out" -prune -o -type f -name "*akefile*" -print

2023年 09月 20日 星期三 13:34:05 CST

./kernel/Makefile

./u-boot/Makefile

./Makefile

test-2:

$ date; mx=2; find . -maxdepth $mx -type d -path "./.git" -prune -o -path "./.repo" -prune -o -path "./out" -prune -o -wholename "./Makefile" -prune -o -type f -name "*akefile*" -print

2023年 09月 20日 星期三 13:36:21 CST

./kernel/Makefile

./u-boot/Makefile

test-3:

$ date; ex=;mx=2; find . -maxdepth $mx -type d -path "./.git" -prune -o -path "./.repo" -prune -o -path "./out" -prune -o -wholename "./Makefile" -prune -o -type f -name "*akefile*"

2023年 09月 20日 星期三 13:37:15 CST

./kernel/Makefile

./out

./u-boot/Makefile

./Makefile

./.git

 

PS: -o 理论上和shell的或符号'||'等价,但又不太一致,它在find的输出中也有与的成分存在,后面有print参数存在时会优先被print与掉,否则依旧会跳过prune的原意来输出!

应该是内部有优先权重在匹配,有空得去看看find的源码是如何实现的。