Is there a good way to grep multiple directories without --include?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Is there a good way to grep multiple directories without --include?
# 1  
Old 07-15-2014
Question Is there a good way to grep multiple directories without --include?

Hello.

I consider myself a journeyman Unix user. I can handle most day to day tasks and have created some fairly complicated command queries, but this problem has defeated me so far. Any help would be greatly appreciated!

We keep an archive in the file system with a directory structure in the form of
Code:
/[EntityName]/[Year]/[1-12]

When we need to find data in the archive we do a recursive search with grep. This worked fine for awhile, but now that we have several years worth of data it is taking too long. I would like to narrow the search by year, but I can't figure out a good way to do so since the year is two directory levels down. I've found answers on Google, but most use "--include" which my version of grep does not allow (AIX).

I have found one command that works, but it is still very slow:
Code:
find ./*/2013/* -type d -exec grep -rl "string to search" {} \;

I also don't understand why I have to include the -r flag, but
Code:
find ./*/2013/* -type d -exec grep -l "191684520" {} \;

does not work

Is there anything I can do to make this go faster? I've experimented with using xargs instead of the -exec flag, but don't really understand it.
# 2  
Old 07-15-2014
You need -r because you are telling find to find directories, and only directories, via -type d.

As for speeding it up it is quite possibly running at maximum speed already -- there's no "extra-fast" way to read a directory short of speeding up the disk. But you could try improving it with xargs so it doesn't run grep more times than it needs to. This may mess up if your file or directory names contain spaces.

Code:
find ./*/2013/* -type f | xargs grep -l "191684520"

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-15-2014
Quote:
Originally Posted by Corona688
You need -r because you are telling find to find directories, and only directories, via -type d.

As for speeding it up it is quite possibly running at maximum speed already -- there's no "extra-fast" way to read a directory short of speeding up the disk. But you could try improving it with xargs so it doesn't run grep more times than it needs to. This may mess up if your file or directory names contain spaces.

Code:
find ./*/2013/* -type f | xargs grep -l "191684520"

I guess I don't understand the -exec flag as well as I thought I did. I thought that I was gathering a list of directories to feed the grep command. Obviously I have a lot to learn. Thank you very much for your help!
# 4  
Old 07-15-2014
If there is any chance that any filenames in your file hierarchy contain whitespace characters (space, tab, or newline), xargs won't work. Even in cases where it will work, using it is usually inefficient. It will probably also go faster if you let find walk the directories instead of having the shell give a longer list of directories to find to process (and may also avoid arg list too long issues). Instead of:
Code:
find ./*/2013/* -type f | xargs grep -l "191684520"

try:
Code:
find ./*/2013/ -type f -exec grep -l "191684520" {} +

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-15-2014
Sometimes even a simple
Code:
grep -l "191684520" */2013/*/*

works.
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 07-15-2014
Quote:
Originally Posted by MadeInGermany
Sometimes even a simple
Code:
grep -l "191684520" */2013/*/*

works.
I could have sworn that I had tried that and it did not work. I must have made a typo somewhere. Thanks for the simple solution.
This User Gave Thanks to pauliesyllabic For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Include information in certain columns using grep and awk

HI all, I have data in a file that looks like this: 1 HOW _ NNP NNP _ 3 nn _ _ 2 DRUGS _ NNP NNP _ 3 nn _ _ 3 ACT _ NNP NNP _ 0 null _ _ 4 : _ ... (3 Replies)
Discussion started by: owwow14
3 Replies

3. Shell Programming and Scripting

Create a dummy file in all directories that include a .jpg

Hello. My latest project has me with the need for the following script. Basically, any directory that includes a .jpg file needs to also have a ".picture" file created (if it doesn't exist). Here's an example of what I need. /mnt/user/Pictures/2011/Hawaii - 2011/.picture... (11 Replies)
Discussion started by: Davinator
11 Replies

4. Programming

where to put include files, multiple src directories

I have an issue with some fortran include files. I have the following src directory structure, trunk/src/client_main trunk/src/client_models trunk/src/server The make file is at trunk/makefile. In /src/client_main there are some included dependencies, COMMON.BLK PARAM.DAT... (0 Replies)
Discussion started by: LMHmedchem
0 Replies

5. UNIX for Dummies Questions & Answers

Deleting multiple directories inside multiple directories

Hi, Very unfamiliar with unix/linux stuff. Our admin is on vacation so, need help very quickly. I have directories (eg 40001, 40002, etc) that each have one subdirectory (01). Each subdir 01 has multiple subdirs (001, 002, 003, etc). They are same in each dir. I need to keep the top and... (7 Replies)
Discussion started by: kkouraus1
7 Replies

6. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

7. Shell Programming and Scripting

Using Grep Include/Exclude Files

I wrote this korn script and ran into a hole. I can use find to exclude all the hidden directories and to use my include file/exclude files for running a full backup find / -depth -ipath '/home/testuser/.*' -prune -o -print| grep -f include.mydirs | grep -v -f exclude.mydirs but when I... (8 Replies)
Discussion started by: metallica1973
8 Replies

8. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

9. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies

10. Shell Programming and Scripting

moving directories to new directories on multiple servers

Hi - I am new to unix scripts...I need to move several directories on multiple servers to new directories. (0 Replies)
Discussion started by: mackdaddy07
0 Replies
Login or Register to Ask a Question