Check if a file exists and has a size greater than N —09 January 2014
You can do this using the find
command and the [[
builtin,
wrapped in a one-liner like this:
[[ $(find /path/to/file -type f -size +51200c 2>/dev/null) ]] && echo true || echo false
- The
find
takes care of two things at once: checks if file exists and size is greater than 51200 bytes. - We redirect stderr to
/dev/null
to hide the error message when the file doesn’t exist. - The output of
find
will be non-blank if the file matched both conditions, otherwise it will be blank. - The
[[ ... ]]
evaluates to true or false if the output offind
is non-blank or blank, respectively.
You can use this in if
conditions, for example:
if [[ $(find /path/to/file -type f -size +51200c 2>/dev/null) ]]; do
# somecmd
fi
Inspired by this question on #stackoverflow.