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] |
Quote:
You may string any number of commands together with pipes: comand1|command2|command3|command4|etc.. |
Maybe have a look at -regex. Also you used the wrong slash in your code block ... should be /code
|
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" 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" If you want to target very specific directories, not all directories that match the given names, you can play with -regex. Yves. |
Hi,
i have used the below, but throwed error as shown Code:
find . -type d \( -name "27thJuly" -o -name "30thJuly" \) -prune -o print0 | |
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" 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:
Code:
/29thJuly/sample1.txt |
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" In alternative you can try with the -regex option, as suggested by grail above, to match the wanted (and only the wanted) files. |
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 '#' |
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:]]*#' |
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? |
Thanks Colucix...
. |
Quote:
Code:
find /home/flamingo \( -wholename /home/flamingo/dir1 -o -wholename /home/flamingo/dir2 \) -prune -o -print 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 |
All times are GMT -5. The time now is 05:16 AM. |