Stole
Stole

Reputation: 5278

Searching for information in files in several directories

I need to check several files which are in different locations for a specific information.

So, how to make a script which checks for the argument word through several directories?

The directories are in different locations. For ex.

/home/check1/

/opt/log/

/var/status/

Upvotes: 2

Views: 107

Answers (5)

Fred Foo
Fred Foo

Reputation: 363607

Use the grep -R (recursive) option and give grep multiple directory arguments.

Upvotes: 1

Francisco Puga
Francisco Puga

Reputation: 25159

The man page of grep should explain what you need. Anyway, if you need to search recursively you can use:

grep -R --include=PATTERN "string_to_search" $directory

You can also use:

--exclude=PATTERN to skip some file
--exclude-dir=PATTERN to skip some directories

The other option is use find to get the files and pipe it to grep to search the strings.

Upvotes: 0

flolo
flolo

Reputation: 15496

You could also do (next to ´find´) do a

for DIR in /home/check1 /opt/log /var/status ; do 
    grep -R searchword $DIR; 
    done

Upvotes: 1

Xavjer
Xavjer

Reputation: 9224

Try find http://content.hccfl.edu/pollock/Unix/FindCmd.htm using your searchwords and the directories.

Upvotes: 0

Julian
Julian

Reputation: 2061

At the very simplest, it boils down to

find . -name '*.c' | xargs grep word

to find a given word in all the .c files in the current directory and below.

grep -R may also work for you, but it can be a problem if you don't want to search all files.

Upvotes: 1

Related Questions