Ubuntu – Using connectors after find command

command linefind

I want my bash to print 'found' only if something is found, using the find command. But using && does not help: even if found nothing, I'm getting 'found' printed.
Example:

$ pwd
/data/data/com.termux/files/home/test/test1/test4
$ ls
xaa  xab
$ find . -name xac && echo 'found'
found
$ find . -name xaa && echo 'found'
./xaa
found

Best Answer

You can make find itself print found:

find . -name xac -printf "found\n" -quit

The -quit will make find quit after the first match, so found is only printed at most once.

On a similar thread on Unix & Linux (make find fail when nothing was found), I used grep -qz to return a non-zero exit status if find found nothing:

find /some/path -print0 -quit | grep -qz .

Which you can use to construct compound commands using && or if:

find /some/path -print0 -quit | grep -qz . && echo found
Related Question