Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

Is there an idiomatic means to detect whether "find" found any matches? I'm currently using

COUNT=`find ... | wc -l`
if [ "$COUNT" -gt 0 ]; then

but this seems a little indirect to me. Also, I'd like find to stop searching once it's found a match, so it doesn't waste time and effort. I just need to know whether or not there are any files that match.

share|improve this question
up vote 15 down vote accepted

If you know you have GNU find, use -quit to make it stop after the first match.

Portably, pipe the output of find into head -n 1. That way find will die of a broken pipe after a few matches (when it's filled head's input buffer).

Either way, you don't need wc to test whether a string is empty, the shell can do it on its own.

if [ -n "$(find … | head -n 1)" ]; then …
share|improve this answer
Interesting... I thought -n only applied to 'defined' variables ($abc, $xyz, etc), but I suppose $(...) is a 'defined' temp variable.. +1 ... (and I just read recently that using -n is risky, unless you are sure that the variable has not already been declared and is 'unused)... but a temp won't have this possibility... ie. uninitialized vs. empty – Peter.O May 27 '11 at 8:09
@fred: On the contrary, -n tests whether a string is empty, it has nothing to do with variables. What you've read was probably saying that using -n doesn't test whether a variable is defined: -n "$foo" is false if foo is defined but empty, and displays an error message under set -u. [ -n "${foo+1}" ] reliably tests whether foo is set, but treating empty and unset variables identically is usually a good idea: [ -n "${foo:+1}" ], or just [ -n "$foo" ] if you're not worried about set -u. – Gilles May 27 '11 at 10:01
Thanks! I don't have -quit available, but piping to head -n 1 works (as does head -c 1, which stops even earlier, but probably with negligible benefit). Also, I made the mistake of writing my question without the code in front of me: I use wc -l in a different case, where I need to know the total number of found files. In the case where I'm only testing for whether there are any matches, I was using if [ -z `find … ` ]. – Chris Page Jun 1 '11 at 7:04

You can use the -quit action to stop after the first match. You'll probably want to combine that with another action (like -print) or you won't be able to tell whether it found anything.

For example, find ... -print -quit will print the path of the first matching file and then exit. Or, you could use -printf 1 -quit to print 1 if there's a match and nothing if there isn't.

find's exit status reflects whether there were errors while searching, and not whether it found anything, so you have to check its output to see if there's a match.

share|improve this answer
-quit is a GNU extension. It won't work on systems using other implementations of find(1), such as OS X, the BSDs, and probably most commercial Unices. – Warren Young May 27 '11 at 1:25
Thanks, that's useful to know in case I'm using another system. Unfortunately, neither -quit nor -printf are available on mine (Mac OS X). – Chris Page May 27 '11 at 1:53

You can wrap it into the shell condition, e.g.:

[ "$(find . ...)" '!=' '' ] && echo Found || echo Not found

Where ... is your match condition, e.g. -name *.txt.

Some other examples:

[ "$(find /etc -name hosts)" ] && echo True || echo False
[ ! -z "$(find /etc -name hosts)" ] && echo True || echo False
share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.