Monday, February 23, 2009

Killing processes which matches grep pattern

Recently I wan working on a script which automatically starts the set of scripts in background.  When I wanted to stop those scripts, I have written a small command which kills all processes at one shot which are matching the pattern. Here is the command

kill -9 `ps -eaf|grep "stat.sh"|grep -v grep | awk '{print $2}'`

Here 
stat.sh ==> is the pattern.
grep -v grep ==> avoid grep's process id
awk '{print $2}'` ==> prints second argument

I use this often in unix/linux.

Monday, February 9, 2009

Linux Unix Find in Files

Following command will search string in files in the directories and sub-directories under current directory.

find . -exec grep "String to be searched" '{}' \; -print

Following command will search string in files which names matches certain criteria.

find . -name '*.html' -exec grep 'String to be searched' {} \; -print

Handy about these is it will print the line containing the file and also file name with path from current directory.

Njoi
Thiru