Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

hi friends I use the following command to find under /var some param in my script

grep -R "param" /var/* 2>/dev/null |grep -wq "param"

my problem is that: after grep find the param in file grep continue to search until all searches under /var/* will completed

How to perform stop immediately after grep match the param word

For example when I run the: grep -R "param" /var/* 2>/dev/null |grep -wq "param"

grep find the param after one second.

But grep continue to Search other same param on other files and its take almost 30 seconds

How to stop the grep immediately after param match?

THX

share|improve this question

1 Answer 1

Use:

grep -m 1

to stop after the first match.

share|improve this answer
    
another quastion if I want to add time out to grep , is it posible? and if not how can I stop grep after 10 seconds. THX –  yael Jun 17 '10 at 11:35
    
@yael: Make it into a script, and at the start of the script put something like: "( sleep 5 ; echo Warning $0 hanging - aborted ; kill -9 $$ ) &; TIMEOUT_PID=$!". At the end of the script put: "kill -9 $TIMEOUT_PID". –  harrymc Jun 17 '10 at 12:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.