toddlermenot
toddlermenot

Reputation: 1618

-prune in find working without OR(-o) option - Unix

$pwd
/tmp
$touch 1.tst 2.tst
$mkdir inner_dir
$touch inner_dir/3.tst
$find . ! -name . -prune -name '*.tst'
1.tst
2.tst

I want to restrict 'find' to search only to the current directory for the files with 'tst' extension (I know this can be done with 'ls' command, but want to add other 'find' filters like mtime later on). My question is how the above 'find' works?. Why doesn't the following work(with an OR option)?

find . ! -name . -prune -o -name '*.tst'

Thanks.

Upvotes: 0

Views: 409

Answers (1)

pedz
pedz

Reputation: 2349

-prune Always evaluates to the value True. Stops the descent of the current path name if it is a directory. If the -depth flag is specified, the -prune flag is ignored.

I think if you play with it, you can figure out what it is doing.

e.g.

find . ! -name . -prune

gives

./1.tst
./2.tst
./d

We don't go down into ./d because of the prune -- "Stops the descent ...". What is left is then filtered by the -name '*.tst' to be just the list files at the top directory.

HTH

Upvotes: 1

Related Questions