What is the difference between -f and -s Option in If condition
Mainly how they are different in functionality?
I know the google meaning of these but want to know the practical use.
From man test:
-f FILE
FILE exists and is a regular file
-s FILE
FILE exists and has a size greater than zero
Let's create a file from scratch and check it out.
$ touch b
Does the file exist?
$ [ -f "b" ] && echo "file exists"
file exists # yes!!!!
Does the file have a size greater than zero?
$ [ -s "b" ] && echo "file exists and is greater than zero"
$ # no!!!!
So a good if-elif-else condition to check the existence of a file could be:
if [ -s "$file" ]; then
echo "exists and it is not empty"
elif [ -f "$file" ]; then
echo "at least exists"
else
echo "does not exist"
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With