user2591612
user2591612

Reputation:

Difference between file tests in Bash

I am troubleshooting an existing Bash script and in the script it has two tests:

if [ ! -s <file_location> ] ; then
   # copy the file to the file_location
   if [ -s <file_location> ] ; then
   # operate on the file
   fi
fi

According to the Bash Tutorial, -s tests if the file is not of zero size. Would it be better to replace the ! -s test with a -e ? I could understand the second, nested test being a -s but the first one looks like it could be replaced with -e. What is the advantage here of ! -s vs -e? Am I missing something?

Upvotes: 0

Views: 72

Answers (1)

chepner
chepner

Reputation: 531888

If the file exists but is empty, -e would pass, but the file would likely be useless. Using ! -s ensures that the file is present and contains useful content.

Upvotes: 2

Related Questions