Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .