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 have very big collection of files. When I use command find I'm sure there is only one file in this collection. To make all my find commands faster I would like to run find command just until it had found one file. I don't need to wait for more files. Maybe some bash script with breaks....

Any idea how to do it?

share|improve this question

migrated from Mar 11 '13 at 10:59

This question came from our site for professional and enthusiast programmers.

up vote 4 down vote accepted

With GNU find you can use the -quit option.

For example, the following command will print the first txt file found and then exit.

find . -name "*.txt" -print -quit
share|improve this answer
Thx. It worked well. – bandit Mar 11 '13 at 10:07

Try reading the manual (man find):


Exit immediately. No child processes will be left running, but no more paths specified on the command line will be processed. For example, find /tmp/foo /tmp/bar -print -quit will print only /tmp/foo. Any command lines which have been built up with -execdir ... {} + will be invoked before find exits. The exit status may or may not be zero, depending on whether an error has already occurred.

share|improve this answer

you could use find with sed:

[za@pc-02 ~]$ find ./ -iname  '[0-9]' | sed 1q
[za@pc-02 ~]$ find ./ -iname  '[0-9]'
[za@apc-02 ~]$ ls
1  2  3  vim

sed 1q - means that you need only one line which produces by find if you need more than one line write this 10q(10 lines)

[0-9] this regexp means match only one digit in range 0-9

share|improve this answer
this answer is not very helpful as is. maybe you can say at least few words to explain what do you really mean and how does it answer the question? – mvp Mar 11 '13 at 10:24
updated with short explanation – Zagorulkin Dmitry Mar 11 '13 at 10:50

You must log in to answer this question.