Tuesday, July 20, 2010

Find a String in UNIX

We've become so much addicted to find/search functionality these days that I tend to look out for Ctrl+F button when I want to find something on a hard bound book as well!!

I had a requirement where I had to search for all the files in my home directory recursively which contained my db2 password because I wanted to change my password in them. I came across this shell command which I found pretty useful:

find . -type f -name "*.*" -exec grep -l "MYDB2PASSWORD" '{}' \;

It just searches through your home directory for a file (type -f) with any name (-name "*.*"). If you are sure of the type of the file you would be interested in then you can also specify it in the -name option (Eg: -name "*.c" for all C programs, etc).

-exec allows us to execute any command in Unix. Here I'm using the Grep command to search for the text MYDB2PASSWORD.

No comments:

Post a Comment