LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-22-2008, 08:35 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 404

Rep: Reputation: 37
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?
 
Old 09-23-2008, 04:18 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
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.
 
Old 09-23-2008, 06:04 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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.

Last edited by jschiwal; 09-23-2008 at 06:07 AM.
 
Old 09-23-2008, 11:21 AM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 404

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by jschiwal View Post
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.
 
Old 09-24-2008, 05:14 AM   #5
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
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);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Solaris + find -prune command puzzle dazdaz Solaris / OpenSolaris 1 02-28-2008 07:29 PM
how does pixel aspect relate to frame size in mplayer/how do you find vid frame size? BrianK Linux - General 3 10-31-2007 02:08 PM
Use the prune option in find command frznchckn Linux - General 1 06-06-2007 08:18 PM
How to prune more than one directory using GNU find judgex Programming 1 06-04-2006 12:50 AM
Need help with some commands to prune files... smeenge Linux - Software 2 05-21-2004 06:10 PM


All times are GMT -5. The time now is 03:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration