Leave "-print" from "find" command when using "-prune"

I could never fully understand the action of the find command -prune. But in fact, at least some of my misunderstandings stem from the effect of the "-print" expression being excluded.

On the "Find" page ..

"If the expression contains no action other than -prune, -print is executed for all files for which the expression is true.

.. which I have always (over the years) thought I could leave "-print".

However, as the following example shows, there is a difference between using '-print' and omitting '-print', at least when the expression '-prune' appears.

First of all, I have the following 8 directories in my working directory.

aqua/
aqua/blue/
blue/
blue/orange/
blue/red/
cyan/blue/
green/
green/yellow/

      

These 8 directories contain 10 files.

aqua/blue/config.txt
aqua/config.txt
blue/config.txt
blue/orange/config.txt
blue/red/config.txt
cyan/blue/config.txt
green/config.txt
green/test.log
green/yellow/config.txt
green/yellow/test.log

      

My goal is to use "find" to list all normal files that don't have blue as part of the file path. There are five files that meet this requirement.

This works as expected.

% find . -path '*blue*' -prune -o -type f -print
./green/test.log
./green/yellow/config.txt
./green/yellow/test.log
./green/config.txt
./aqua/config.txt

      

But when I leave "-print", it returns not only the five desired files, but any directory whose path name contains "blue".

% find . -path '*blue*' -prune -o -type f
./green/test.log
./green/yellow/config.txt
./green/yellow/test.log
./green/config.txt
./cyan/blue
./blue
./aqua/blue
./aqua/config.txt

      

So why are the three blue directories displayed?

This can be important because I often try to shorten a directory structure that contains over 50,000 files. When this path is being processed, my find command, especially if I do "-exec grep" for each file, can take in a huge amount of time processing files for which I have absolutely no interest. I need to be sure that the search does not go into the edged structure.

+3


source to share


1 answer


Implicit -print

applies to the entire expression, not just the last part of it.

% find . \( -path '*blue*' -prune -o -type f \) -print
./green/test.log
./green/yellow/config.txt
./green/yellow/test.log
./green/config.txt
./cyan/blue
./blue
./aqua/blue
./aqua/config.txt

      

It doesn't go into truncated directories, but prints the top level.



Small modification:

$ find . ! \( -path '*blue*' -prune \) -type f
./green/test.log
./green/yellow/config.txt
./green/yellow/test.log
./green/config.txt
./aqua/config.txt

      

(with implicit -a

) will result in the same behavior with and without -print

.

0


source







All Articles