Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if the output of the "find" command is empty?

Tags:

I want to return an exit status of 0 if the output is empty and 1 otherwise:

find /this/is/a/path/ -name core.*
like image 525
cstack Avatar asked Jun 29 '11 21:06

cstack


People also ask

How do you check if output of a command is empty or not?

Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

How check directory is empty in Linux?

There are many ways to find out if a directory is empty or not under Linux and Unix bash shell. You can use the find command to list only files. In this example, find command will only print file name from /tmp. If there is no output, directory is empty.

How check if file is empty Linux?

You can use the find command and other options as follows. The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data.


1 Answers

When you say you want it to return a particular number, are you referring to the exit status? If so:

[[ -z `find /this/is/a/path/ -name core.*` ]]

And since you only care about a yes/no response, you may want to change your find to this:

[[ -z `find /this/is/a/path/ -name core.* -print -quit` ]]

which will stop after the first core file found. Without that, if the root directory is large, the find could take a while.

like image 82
Rob Davis Avatar answered Oct 25 '22 18:10

Rob Davis