3

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?

3 Answers 3

7

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
1
  • Thx. It worked well.
    – bandit
    Mar 11, 2013 at 10:07
0

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.

0

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

2
  • 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, 2013 at 10:24
  • updated with short explanation Mar 11, 2013 at 10:50

You must log in to answer this question.