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 printfound
:The
-quit
will makefind
quit after the first match, sofound
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 iffind
found nothing:Which you can use to construct compound commands using
&&
orif
: