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 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 stackoverflow.com Mar 11 '13 at 10:59

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

add comment

3 Answers 3

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

Try reading the manual (man find):

-quit

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

you could use find with sed:

[za@pc-02 ~]$ find ./ -iname  '[0-9]' | sed 1q
./1
[za@pc-02 ~]$ find ./ -iname  '[0-9]'
./1
./3
./2
[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
add comment

Your Answer

 
discard

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