Grep For More Than One Word Recursively

I have an application that runs on AIX that is nothing short of ugly when it comes to pointing it to a different datasource.  This application currently uses an outdated version of Savvion BPM that requires manual editing of any config file that contains the datasource and/or credentials.  To complicate things further, our application which runs in WebSphere also has some config files that need manual updating.

Today…I decided to try and document the process as to save me and anyone else who comes behind me some serious grief.

Because AIX’s version of grep doesn’t have a -R option, I will give you syntax that *should* work on any flavor of UNIX/Linux with grep.

find /path/to/parent/directory -type f -print | xargs grep -i -E 'word1|credential1|server1|server2|server3' > /path/to/output/file

Let’s dissect this for clarification:

find /path/to/parent/directory -type f -print

We are telling the find command where we want grep to search.  You can either specify the explicit path for example, I searched under /opt, or you can change directory to the parent directory then issue a “.” (sans double quotes) to tell find to “search from here”.

The -type f simply tells find to locate regular text files, not binary files.

The -print is important.  It will print the full path to the file.  The reason this is important is because when grep finds a match in one of the files, it will print the full path out in addition to the line it found within the file so you know exactly where the file is located for future reference.

xargs

xargs simply is a command that allows you to issue any command (grep in our case) against the list of standard-in (list of regular files) one at a time.  As if you did it one at a time manually.

grep -i -E 'word1|credential1|server1|server2|server3'

grep is a command that allows you to search the contents of a file for a specific pattern.  The -i tells grep that this search is case insensitive.  The -E tells grep that we want to use regular expressions in our search.

We then follow with a basic regular expression.  Our search is in single quotes and each word we want to search for is separated by the pipe symbol.  We are just asking grep to find any word that matches in the list.

> /path/to/output/file

Here  we are just redirecting the results of our search to a file so that we can review and modify as we deem necessary.  Remember that if you already have data in the destination file, a > will overwrite the contents with the new data found.  A >> will just append to the file.

Once this command has finished, you will be able to edit the file and remove any false positives that grep might have come across.

Some what dirty, but a simple solution.

-boogybren