0

I want to check if the three files that were created are empty or not using AND operator.

#!/bin/bash
FILE_1="/root/Desktop/file1"
FILE_2="/root/Desktop/file2"
FILE_3="/root/Desktop/file3"

if [[ -f "${FILE_1}" &&  -f "${FILE_2}" && -f "${FILE_3}" ]]; then
  echo "All files are not empty"
else
  echo "All files are empty"
fi 

But I not getting an expected output. What will be the correct syntax to use AND operator in if-else statement?

1 Answer 1

2

-f checks whether the file exists and is a regular file (i.e. not a link, directory, socket etc.). To check whether it's empty you need to employ -s as well which checks the file's size:

if [[ -f $FILE_1 && ! -s $FILE_1
   && -f $FILE_2 && ! -s $FILE_2
   && -f $FILE_3 && ! -s $FILE_3
]] ; then

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .