LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-28-2013, 06:53 AM   #1
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Rep: Reputation: 9
Prune not exculding directory while finding files based on size


Hi,

I am trying to find files with size more than 5 megs but excluding some particular using prune, how the code is not working and directoris are not getting excluded. Below is my code

Code:
# find / -xdev  -type f -size +5M  | xargs ls -l

-rw-r--r-- 1 root root 17992154 Aug  7 23:12 /etc/test1
-rw------- 1 root root 14123295 Aug  7 23:12 /etc/test2
-rw-r--r-- 1 root root 17752583 Aug  7 23:08 /etc/test3
-rw------- 1 root root 14069493 Aug  7 23:08 /etc/test4
-r--r--r-- 1 root root  5811146 Jan 20  2012 /lib64/test5
Below is the code when i use prune

Code:
find / -xdev \( -path './lib64/' -prune \) -o -type f -size +5M  | xargs ls -l

-rw-r--r-- 1 root root 17992154 Aug  7 23:12 /etc/test1
-rw------- 1 root root 14123295 Aug  7 23:12 /etc/test2
-rw-r--r-- 1 root root 17752583 Aug  7 23:08  /etc/test3
-rw------- 1 root root 14069493 Aug  7 23:08 /etc/test4
-r--r--r-- 1 root root  5811146 Jan 20  2012 /lib64/test5
So can anyone suggest what i am missing or doing wrong here.
 
Old 01-28-2013, 07:49 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The path of the directory to exclude (given as argument to -path) should be exactly the same as the path seen by find. Since it starts the search from the / directory, it should start with / as well and it should not have the trailing slash:
Code:
-path /lib64
Moreover, the rule about -prune is "if the expression contains no actions other than -prune, -print is performed on all files for which the expression is true". In your command you have no other actions and if you provide a correct path to exclude (as shown above) the name of the directory /lib64 will still be shown. You have to add at least the default action -print explicitly if you want to see the correct output.

Finally there is no need of escaped parentheses in this case, hence your command line should read:
Code:
find / -xdev -path '/lib64' -prune -o -type f -size +5M -print
Hope this helps.
 
1 members found this post helpful.
Old 01-29-2013, 01:18 AM   #3
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Original Poster
Rep: Reputation: 9
I totally agree with you and it does helped me, however i am having some doubt in understanding below

Quote:
Originally Posted by colucix View Post
Moreover, the rule about -prune is "if the expression contains no actions other than -prune, -print is performed on all files for which the expression is true". In your command you have no other actions and if you provide a correct path to exclude (as shown above) the name of the directory /lib64 will still be shown. You have to add at least the default action -print explicitly if you want to see the correct output.
1. "if the expression contains no actions other than -prune, -print is performed on all files for which the expression is true" ? Could you please tell me what is the exact order in which the above conditions in "find command" gets executed. I see the below output and in that /lib64 is printed at last; why is that since it is appearing before in "find command" shouldn't it be printed first.

Code:
 find / -xdev -path '/lib64' -prune -o -type f -size +5M

/etc/test1
/etc/test2
/etc/test3
/etc/test4
/lib/test5
/lib64
2. If print is anyway the default action then why to specify it explicitly ?
 
Old 01-29-2013, 11:02 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,136

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
[QUOTE=Rohit_4739;4879595]
Code:
 find / -xdev -path '/lib64' -prune -o -type f -size +5M

/etc/test1
/etc/test2
/etc/test3
/etc/test4
/lib/test5
/lib64
In that expression, "-prune" will be TRUE for the directory "/lib64", and therefore the entire expression is TRUE and you get the default action of "-print".
Code:
 find / -xdev -path '/lib64' -prune -o -type f -size +5M -print
This expression does contain an action other than "-prune", and your explicit "-print" will only occur when the second term of the expression is matched.

It becomes clearer if you add the implied parentheses:
Code:
 find / -xdev \( -path '/lib64' -prune \) -o \( -type f -size +5M -print \)
Quote:
2. If print is anyway the default action then why to specify it explicitly ?
See above.

Last edited by rknichols; 01-29-2013 at 11:29 AM. Reason: Add the "It becomes clearer..."
 
  


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
Finding Directory Size Manjunath1847 Linux - Software 5 10-27-2010 05:54 AM
find using -prune and -size sharky Programming 4 09-24-2008 05:14 AM
finding size of total files in a directory blackzone Linux - Newbie 3 01-07-2005 04:01 AM
Finding files via their size ivanatora Linux - General 1 09-08-2004 09:23 AM
Finding size of a directory chrisk5527 Linux - General 2 12-30-2003 09:49 AM


All times are GMT -5. The time now is 04:13 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration