LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   find using -prune and -size (https://www.linuxquestions.org/questions/programming-9/find-using-prune-and-size-671686/)

sharky 09-22-2008 07:35 PM

find using -prune and -size
 
I can't seem to get find to use both -prune and -size together. I want to find files over a certain size and at the same time exclude certain directories.

I'm able to do one or the other but not both simultaneously.

To find files of a certain size I use this in a bash script.

Code:

find $1 -user $USER -type f -size +600k -exec ls -lh {} \;
To exclude certain paths in the find I use this.

Code:

find $1 -user $USER -type f -size +100k -path $1/.snapshot -prune -o -print -exec ls -lh {} \;
The second statement excludes the .snapshot directory but I'm getting all file sizes returned and not just the ones over 100k. It also appears that the -user flag is ignored.

What am I doing wrong?

burschik 09-23-2008 03:18 AM

By default, find combines its arguments using AND. And since AND binds more tightly than OR, your statement is not doing what you think it is.

jschiwal 09-23-2008 05:04 AM

You need to put prune before the other tests and precede the other tests with -o. The -prune option prevents decending the directory and returns true.

Here is an example which excludes the ./udev directory:
Code:

find ./ \( -path './udev' -prune \) -o -type f -name "*.war" -print
Without the -print at the end the directory, './udev' is printed but its files are not.
Here I'll locate all "*.war" files excluding both ./udev and ./webarchives directories:
Code:

find ./ \( -path './udev' -o -path './webarchives' \) -prune -o -type f -name "*.war" -print
Start of with just the -path tests and make sure that each directory you want excluded are printed out. Then group them (if there is more than one -path test); add -prune ; add your normal test and finally add -print at the end.

sharky 09-23-2008 10:21 AM

Quote:

Originally Posted by jschiwal (Post 3289083)
You need to put prune before the other tests and precede the other tests with -o. The -prune option prevents decending the directory and returns true.

Here is an example which excludes the ./udev directory:
Code:

find ./ \( -path './udev' -prune \) -o -type f -name "*.war" -print
Without the -print at the end the directory, './udev' is printed but its files are not.
Here I'll locate all "*.war" files excluding both ./udev and ./webarchives directories:
Code:

find ./ \( -path './udev' -o -path './webarchives' \) -prune -o -type f -name "*.war" -print
Start of with just the -path tests and make sure that each directory you want excluded are printed out. Then group them (if there is more than one -path test); add -prune ; add your normal test and finally add -print at the end.

I'm still not clear on how it works but I was able to use your examples to get something working for me. Thanks.

burschik 09-24-2008 04:14 AM

Code:

find ./ \( -path './udev' -o -path './webarchives' \) -prune -o -type f -name "*.war" -print
This means:

Code:

if (path == "./udev" || path == "./webarchives") {
    prune;
} else if (type == f && name == "*.war") {
    print;
}

However, find uses AND instead of if.. then.. else:

Code:

((path == "./udev" ||path == "./webarchives") && prune) ||
((type == f && name == "*.war") && print);



All times are GMT -5. The time now is 11:59 AM.