Take the 2-minute tour ×

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
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

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

Your Answer


