how to enable find command to print exe status diff from 0 when find command not find the file?
according to my example when I try to find the test1.txt file , $? set to 0 but I expect to get value diff from 0 because the file not found
[root@om-1 tmp]# touch test.txt
[root@om-1 tmp]# find /var/tmp -name test.txt
/var/tmp/test.txt
[root@om-1 tmp]# echo $?
0
[root@om-1 tmp]# find /var/tmp -name test1.txt (test1.txt not under /var/tmp)
[root@om-1 tmp]# echo $?
0
You can't.
From the
find
man page:The exit status
0
just says: I managed to process all files without error (e.g. permission problems).One solution:
Whereas locate returns 1 when the file is not found in the database
Try the following shell condition:
which is basically the same as:
The exit status of find should be zero in both cases, because the find command did not fail. In case it did not find any match, it just exists normally without printing any output.
As a workaround, you can use something like:
However, I am not sure about the result in case of error.
Is using
find
mandatory for you? Do you exactly know where some file should be? Then for exampleshould work for you.