LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   exit status of command "find" (https://www.linuxquestions.org/questions/linux-newbie-8/exit-status-of-command-find-767774/)

sarturbest 11-08-2009 04:09 PM

exit status of command "find"
 
Hi all,
I am beginner in Linux.I want to do the following (bash shell):

if "find" command finds 'thereis' file in the current directory then "echo" Yes otherwise No.

Any ideas?

I tried to use exit status of "find" but it gives the same result in both cases.

choogendyk 11-08-2009 04:27 PM

Check the man page for find. The definition of the return status is rather broad, but it basically refers to all files having been processed successfully, not whether it found what you were looking for. There are other ways in the shell of asking whether a file exists or not.

Robhogg 11-08-2009 04:33 PM

Yes, exit status won't do it here. A slightly messy indirect way would be to count the number of lines (matches) that find outputs:

Code:

if test `find . -name $filename | wc -l` -gt 0; then
  echo yes
else
  echo no
fi

However, if it's just a simple file existence test that you're after, bash has built-in conditional operators to do this:

Code:

if [ -e $filename ]; then
  echo yes
else
  echo no
fi

There are several such operators (see man bash for more).

sarturbest 11-09-2009 02:52 AM

Thanks


All times are GMT -5. The time now is 06:28 AM.