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.

I am trying to use find to locate a single directory, and I would like it to stop as soon as it finds something, is this possible?

Currently I am using the command find /directory/to/search/ -maxdepth 4 -name *search_text* -type d -prune Which cuts the run time down a decent amount but it still continues for a while after it finds what I am looking for. Since this is being run in a script I can't simply Ctrl+c to stop it. Any suggestions?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Sure just run

find /directory/to/search/ -maxdepth 4 -name "*search_text*" -type d -prune -print -quit

check out the man page for find and its actions section

share|improve this answer
    
That's perfect! I don't know how I missed this in the man page. Thanks! –  EEP Oct 12 '12 at 14:40

I don't think that's directly possible; find doesn't have any option for it.

I found this snippet which kills find when it reaches the second result:

find /directory/to/search/ -name *search_text* -print -exec perl -e 'kill "INT", getppid' \;
share|improve this answer
find ... | head -1

As soon as head has read one line it will close the pipe which will kill the find.

share|improve this answer

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.