LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack Search this Thread
Old 07-30-2010, 05:22 AM   #1
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Rep: Reputation: 0
Excluding multiple directories


hi,

I want to exclude multiple directories given as input in grep command.

Can anybody help me in this.

The following will exclude only one directory 27thJuly

[CODE]find . \( -type d -name "27thJuly" -prune \) -o -print| grep -rnH "root" *[\CODE]
 
Old 07-30-2010, 06:20 AM   #2
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215
Blog Entries: 2

Rep: Reputation: 49
Quote:
Originally Posted by flamingo_l View Post
I want to exclude multiple directories given as input in grep command.
Don't quite follow but 'grep -v something' will exclude every line with the word 'something' in it.

You may string any number of commands together with pipes: comand1|command2|command3|command4|etc..
 
Old 07-30-2010, 07:18 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,176

Rep: Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779
Maybe have a look at -regex. Also you used the wrong slash in your code block ... should be /code
 
Old 07-30-2010, 07:33 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
The example command you gave would in fact grep for the word “root” among the filenames output by find. I suppose you actually want to grep the files themselves. The correct syntax for your example command would therefore be:
Code:
find . \( -type d -name "27thJuly" -prune \) -o -print0 | xargs -0 grep -nH "root"
(although neither the parenthesis nor the -H are strictly required)

For multiple directories, any of these commands should be suitable:
Code:
find . -type d \( -name "dir1" -o -name "dir2" \) -prune -o print0 | xargs -0 grep -nH "root"
find . -type d -name "dir1" -prune -o -type d -name "dir2" -prune -o print0 | xargs -0 grep -nH "root"
find . -type d \( -name "dir1" -o -name "dir2" \) -prune -o grep -nH {} +
The latter will perform better if there are a lot of files; in this case, the -H option is necessary. In case of incompatibility with your version of find, you can replace the “+” with “\;”.
If you want to target very specific directories, not all directories that match the given names, you can play with -regex.

Yves.
 
Old 08-02-2010, 06:48 AM   #5
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Hi,

i have used the below, but throwed error as shown

Code:
 find . -type d \( -name "27thJuly" -o -name "30thJuly" \) -prune -o print0 |
xargs -0 grep -nH "abcd"
find: paths must precede expression: print0
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|opt] [path...] [e
xpression]
 
Old 08-02-2010, 07:18 AM   #6
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
The command is working when am using without print0 as follows..

Code:
find . -type d \( -name "27thJuly" -o -name "30thJuly" \) -prune -o -print|xargs -0 grep -nH "abcd"
But it is not producing the correct output:
Suppose there are 3 directories in my current dir. Am excluding 2 directories from search, the other directory has multiple files but only 1 file contains the word "abcd", then it is showing are the files in that directory as shown below:

Code:
$  find . -type d \( -name "27thJuly" -o -name "30thJuly" \) -prune -o -print|xargs -0 grep -nH "abcd"
grep: .
./29thJuly
./29thJuly/RP_search.txt
./29thJuly/RP_search_client.sh
./29thJuly/sample1.txt
: No such file or directory
Output should be:

Code:
/29thJuly/sample1.txt
 
Old 08-02-2010, 07:37 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
A correct way to exclude directories with -prune might be
Code:
find . \( -wholename ./27thJuly -o -wholename ./30thJuly \) -prune -o -type f -print0 | xargs -0 grep -l "abcd"
The -type f option excludes the directory names from the output and the -print0 option manages file names with spaces (as previously mentioned).

In alternative you can try with the -regex option, as suggested by grail above, to match the wanted (and only the wanted) files.
 
1 members found this post helpful.
Old 08-03-2010, 01:06 AM   #8
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
hi Colucix, Thank you ... Its working...

In the result of the above grep i want to exclude line containing # i.e comments from the search result.

The below command is working , is there any other way to do this

Code:
find . \( -wholename ./27thJuly -o -wholename ./30thJuly \) -prune -o -type f -print0 | xargs -0 grep -l "abcd"|grep -v '#'
 
Old 08-03-2010, 02:19 AM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Well, if comments are preceded by one or more blank spaces or tabs, you may refine the regexp with something like:
Code:
... | grep -v '^[[:space:]]*#'
This matches (and exclude) any line containing 0 or any number of space characters followed by # at the beginning of the line itself (see the ^ anchor).
 
Old 08-03-2010, 02:21 AM   #10
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
I have written a script which takes the directories to be excluded as input.

Am running the script in different path, so am giving the directories to be excluded relative to the path which am running the script.

like for example --> ../../sample_files/EX1 and ../../sample_files/EX2

when i run the above command it is not excluding the directories

Can anybody please help me here?
 
Old 08-03-2010, 02:28 AM   #11
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Thanks Colucix...

.
 
Old 08-03-2010, 03:45 AM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
Originally Posted by flamingo_l View Post
Am running the script in different path, so am giving the directories to be excluded relative to the path which am running the script.

like for example --> ../../sample_files/EX1 and ../../sample_files/EX2
The -wholename predicate matches paths relative to the top level (search) directory as specified in the find command. In this case better to use absolute paths, e.g.
Code:
find /home/flamingo \( -wholename /home/flamingo/dir1 -o -wholename /home/flamingo/dir2 \) -prune -o -print
This works despite the current working directory from which you run the script. Just be sure to use an absolute search path (highlighted in blue in my example) in the find command line.

Moreover, if you want to easily transform the list of directories to their absolute paths, you may go to the directory where you run the script and do something like this:
Code:
while read line
do
  readlink -f "$line"
done < original_list > new_list
where I assume the input file is called "original_list". The readlink -f command gives the absolute path of any file/directory/link. Hope this helps.
 
1 members found this post helpful.
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
excluding directories from commands David the H. Linux - General 5 04-06-2009 03:21 PM
Excluding directories from tar? LocoMojo Linux - Software 6 01-24-2006 01:01 AM
Find excluding multiple directories with a wildcard pteigeler Linux - Software 1 09-02-2005 10:57 AM
tar, excluding directories recursively towlie Linux - Newbie 3 06-17-2004 12:32 PM
Excluding directories from zip files jonathanztaub Linux - General 1 05-17-2004 02:17 AM


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

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