Linux - General This forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
09-16-2004, 04:56 AM
#1
Member
Registered: Sep 2004
Posts: 172
Thanked: 0
Using find and grep - how to exclude a result ?
[
Log in to
get rid of this advertisement]
Hello
I am searching all files created from user "daniel" using
find / -user daniel
but I wish to remove/exclude from results all lines containing /home/daniel
Is it possible adding a grep command ? Or any other way ?
Thank you
09-16-2004, 05:04 AM
#2
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Mandriva 2009
Posts: 1,831
Thanked: 16
find / \( -type d -regex "/home/daniel" -prune \) -o -user daniel -print
Yves.
09-16-2004, 05:06 AM
#3
Member
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555
Thanked: 0
Hi Daniel (if that's your real name). Welcome to LinuxQuestions!
The -v flag to grep inverts your search. Grep output can be sent to another grep for further filtering. So, to find all files with a name containing martin excluding /home/daniel, you would write
# find / -user daniel | grep -v /home/daniel | grep martin
Martin
09-16-2004, 05:07 AM
#4
Member
Registered: Sep 2004
Posts: 172
Thanked: 0
Original Poster
Thank you , I was not expecting a so fast reply , thank you all !
Graziano
09-16-2004, 05:19 AM
#5
Member
Registered: Sep 2004
Posts: 172
Thanked: 0
Original Poster
Quote:
Originally posted by theYinYeti
find / \( -type d -regex "/home/daniel" -prune \) -o -user daniel -print
Yves.
this solution is interesting because it doesn't use grep -v (so probably it uses less cpu and it's more fast) .
But suppose I wish to exclude from search more parameters , for example
"/home/daniel" and "/backup) ...
I tried
find / \( -type d -regex "/home/daniel" -prune \) \( -type d -regex "/backup" -prune \) -o -user daniel -print
find / \( -type d -regex "/home/daniel" | "/backup" -prune \) -o -user daniel -print
and other ways .. but no solution . Is it possible to exclude multiple values with find ?
Thank you
09-16-2004, 05:29 AM
#6
Member
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555
Thanked: 0
This is maybe not the answer to your question, but might be valid anyway. If you search with 'find' and 'grep', I don't think that the CPU but the disk transfer speed is what's holding you back. Just listen to the poor HDD when you use find!
Martin
09-16-2004, 05:33 AM
#7
Member
Registered: Sep 2004
Posts: 172
Thanked: 0
Original Poster
infact my idle goes to 0 and IOwait near 99%
I suppose there is no way to limit .. I tried to renice but no useful ...
09-16-2004, 05:35 AM
#8
Member
Registered: Sep 2004
Posts: 172
Thanked: 0
Original Poster
however using find with multimple exclusion should be
find / \( -type d -regex "/home/daniel" -prune -regex "/backup" -prune \) -o -user daniel -print
I tried it now and as it seems works fine.
09-16-2004, 05:36 AM
#9
Member
Registered: Sep 2004
Posts: 172
Thanked: 0
Original Poster
no it doesn't work too , it shows also lines with /backup ...
09-16-2004, 05:42 AM
#10
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Mandriva 2009
Posts: 1,831
Thanked: 16
I can't test, but I would try one of those:
find / \( -type d -regex "/home/daniel" -prune \) -o \( -type d -regex "/backup" -prune \) -o -user daniel -print
find / \( -type d -regex '\(/home/daniel\)\|\(/backup\)' -prune \) -o -user daniel -print
Yves.
Last edited by theYinYeti; 09-16-2004 at 05:43 AM ..
09-16-2004, 05:45 AM
#11
Member
Registered: Sep 2004
Posts: 172
Thanked: 0
Original Poster
Quote:
Originally posted by theYinYeti
I can't test, but I would try one of those:
find / \( -type d -regex "/home/daniel" -prune \) -o \( -type d -regex "/backup" -prune \) -o -user daniel -print
find / \( -type d -regex '\(/home/daniel\)\|\(/backup\)' -prune \) -o -user daniel -print
Yves.
Thank you , I am testing the 2th just now .
09-16-2004, 05:49 AM
#12
Member
Registered: Sep 2004
Posts: 172
Thanked: 0
Original Poster
find / \( -type d -regex '\(/home/daniel\)\|\(/backup\)' -prune \) -o -user daniel -print
works great . It seems faster than find / -user daniel | grep -v '/home/daniel' | grep -v '/backup'
Thank you
06-29-2005, 10:47 AM
#13
LQ Newbie
Registered: Jun 2005
Posts: 3
Thanked: 0
I think you got lucky.
What happens if you reverse the order of the arguments?
find / -user daniel -o \( -type d -regex '\(/home/daniel\)\|\(/backup\)' -prune \) -print
I think it will return no results. If that is the case, then I'm baffled as to what's going on.
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 05:43 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News
LQ Podcast
LQ Radio