LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 03-02-2007, 05:39 AM   #1
LinuxGeek
Member
 
Registered: Jun 2002
Posts: 302

Rep: Reputation: 31
Telling find command to ignore directories


Hi,
I'm trying to get the find command to ignore certain directories when searching. I looked in the man page and it recommended -wholename and prune but I tried both and they don't seem to work. What I'm trying to do is something like this:

find / -iname \*.iso

but I don't want to search in certain directories such as /share, etc. (all of them are on the same filesystem so I can't use -xdev). Any ideas? Thanks for your help.
 
Old 03-02-2007, 05:54 AM   #2
pk2001
LQ Newbie
 
Registered: Feb 2007
Location: Singapore
Distribution: Debian Sarge/Etch
Posts: 19

Rep: Reputation: 0
Quote:
Originally Posted by LinuxGeek
Hi,
I'm trying to get the find command to ignore certain directories when searching. I looked in the man page and it recommended -wholename and prune but I tried both and they don't seem to work. What I'm trying to do is something like this:

find / -iname \*.iso

but I don't want to search in certain directories such as /share, etc. (all of them are on the same filesystem so I can't use -xdev). Any ideas? Thanks for your help.
I had to do something similar recently. Also I could not find a way to instruct "find" to ignore directories.... However, I solved this by piping the find result to "grep" and instructing grep to ignore certain keywords. For example:

Quote:
Originally Posted by pk2001
find / -name "*.txt"|grep -ve "/usr/share/" -e "/usr/lib/" -e "/var/www/"
This find command will find all the text files that exist in the root directory and any subdirectories, EXCEPT in /usr/share, /usr/lib/ and /var/www

Bear in mind that it is the "-v" switch for grep that really saves you here.
 
Old 03-02-2007, 05:55 AM   #3
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 129Reputation: 129
You can specify the directories you do want to search. For example:

find /home /var /tmp -name '*iso'
 
Old 03-02-2007, 10:01 AM   #4
LinuxGeek
Member
 
Registered: Jun 2002
Posts: 302

Original Poster
Rep: Reputation: 31
Thanks for your replies. It seems that it's best to pipe to grep or use xargs. jay73, the problem with the suggested approach above is if the directory you want to exclude is a subdirectory of a directory that you want to search. In that case (which is my case), you would have to either:

a) use grep

b) use xargs

c) try to find out how to do it natively with find
 
Old 03-02-2007, 07:52 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
I think the -wholename -prune approach is the most reliable! Let me do an example. Suppose I have a directory called /test containing three files
Code:
> ls -1 -F /test
apple
pine
pineapple
If I issue the find command I will get four entries
Code:
> find /test
/test
/test/pine
/test/pineapple
/test/apple
Now, let's start from the option -wholename pattern. Only the entries matching the pattern will be listed. The whole entries!!!
Code:
> find /test -wholename pine
>
Nothing! Indeed, there is no entry that matches exactly pine. Now, let's add wildcards to the pattern.
Code:
> find /test -wholename \*pine\*
/test/pine
/test/pineapple
Here there are two entries that match pine with something before and/or something after! Now, let's create a directory inside /test and put duplicates of the same three files inside it. Then issue the find command.
Code:
> mkdir /test/newdir
> touch /test/newdir/pine
> touch /test/newdir/pineapple
> touch /test/newdir/apple
> find /test
/test
/test/pine
/test/newdir
/test/newdir/pine
/test/newdir/pineapple
/test/newdir/apple
/test/pineapple
/test/apple
Now, let's try the find command with the two wildcards again. Then issue the find command using the name of a directory as pattern!
Code:
> find /test -wholename \*pine\*
/test/pine
/test/newdir/pine
/test/newdir/pineapple
/test/pineapple
> find /test -wholename /test/newdir
/test/newdir
The former has retrieved two of the new files, as expected. The latter has retrieved the entry matching exactly /test/newdir.
Now the intersting part: let's introduce the option -prune (literally to cut off branches from a tree)! First note that -prune is an ACTION (like -print) and not a TEST (like -wholename). This means that -prune performs a task, it is not something to evaluate! This task is: if an entry is a directory do not descend into it. Let's try
Code:
find /test -wholename /test/newdir -prune
/test/newdir
here the test is true (the entry /test/newdir matches the pattern) but since we have not searched for anything else, the result is not meaningful. Actually the rule is if the expression contains no actions other than -prune, -print is performed on all files for which the expression is true.
The utility of -prune is clear, if we add another expression by means of the OPERATOR expr1 -o expr2
Code:
> find /test -wholename /test/newdir -prune -o -print
/test
/test/pine
/test/pineapple
/test/apple
here we have -prune and -print, so the /test/newdir entry is not printed anymore; the directory /test/newdir is excluded from the search path; any other entry whose wholename does not match exactly /test/newdir is printed. Now add a TEST to the second expression
Code:
> find /test -wholename /test/newdir -prune -o -name apple -print
/test/apple
the trick is done! We have found a file called apple excluding that one inside /test/newdir. If you want to exclude more than one directory you can do
Code:
find /test \( -wholename /test/newdir -o -wholename /test/pippo \) -prune -o -name apple -print
that is in the first expression you will put two TESTS enclosed in escaped ( ) and separated by the OPERATOR -o. Finally, in your example you will have something like
Code:
find / \( -wholename /share -o -wholename /lib -o -wholename /bin \) -prune -o -iname \*.iso -print
I hope these options are a little more clear now, despite my bad english! Sorry for the very long post!

Last edited by colucix; 01-09-2010 at 07:33 AM. Reason: Escaped * to avoid shell globbing
 
1 members found this post helpful.
Old 03-02-2007, 08:33 PM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If you use grep to filter the output, the directories like /usr will still be searched which may take some time.
It would be better having an idea on which part of the filesystem the file exists in.
The command "find /home/ /tmp -iname "*.iso" would be better then "find / "*.iso".

If you install the source for the find command, you can use the "make pdf" target to generate the "Finding Files" document from the texinfo source. It is 60 pages long.
 
Old 03-03-2007, 01:28 AM   #7
LinuxGeek
Member
 
Registered: Jun 2002
Posts: 302

Original Poster
Rep: Reputation: 31
Thank you very very much colucix for your detailed example Thank you also jschiwal for your comment.
 
Old 04-28-2008, 01:35 PM   #8
sharky
Member
 
Registered: Oct 2002
Posts: 366

Rep: Reputation: 37
type is ignored

I used a command somewhat like this to filter out some huge directories.

find . -type d \( -name '*core*' \) -prune -o -print

All directories with *core* in the name were pruned as I desired but the final output included files and directories. Why would the -type switch be ignored?
 
Old 04-28-2008, 01:56 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 69
Quote:
Originally Posted by sharky View Post
Why would the -type switch be ignored?
It is in the first branch of an “or” test. So it is like,
Code:
if(matches)
    prune
else
    print
Since we are reviving this thread, I might point out a much simpler alternative to using prune is just a negated pattern match. E.g.,
Code:
find / ! -wholename '*/share/*' -name '*.iso'
(where the ‘!’ may or may not need escaping).

Last edited by osor; 04-28-2008 at 01:58 PM.
 
Old 11-10-2009, 12:35 PM   #10
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
On the system I'm using 'find' doesn't have '-wholename' predicate. (apparently older version of binutils). I try to skip directories that start with '.svn', and that worked for me:

Quote:
find . -type d \( -name '\.svn*' \) -prune -o -type d -print
I am not sure how and why it works in this shape and form, but it is worthwhile trying.

Last edited by rsashok; 11-10-2009 at 12:38 PM.
 
Old 08-17-2011, 08:39 AM   #11
amarh123
LQ Newbie
 
Registered: Aug 2011
Posts: 1

Rep: Reputation: Disabled
find [source dir] -path [source dir]/[sub dir 1] -prune -o -path [source dir]/[sub dir 2] -prune -o -name [filename]
 
  


Reply

Tags
find, grep, ignore, piping


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ignore specific sub directories when using tar MicahCarrick Linux - Software 4 11-16-2006 09:59 AM
mount command cant find smb directories but file-browsers can map to them. robertbas Linux - Networking 2 09-21-2006 03:12 AM
How can I find in only particular directories? nadavvin Linux - General 3 08-05-2006 01:48 AM
cp command: except or ignore option? C-RAF. Programming 9 02-28-2006 03:23 AM
Any arguement that can be used with 'ls' command to ignore case? davidas Linux - Newbie 5 02-26-2006 04:46 AM


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

Main Menu
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