how to check If the file Is Empty or not using shell script?
-s FILE
FILE exists and has a size greater than zero
Example 1:
touch /$HOME/f1
echo "data" >/$HOME/f2
ls -l /$HOME/f{1,2}
[ -s /$HOME/f1 ]
echo $?
Sample outputs:
1
The non-zero output indicates that the file is empty.
[ -s /$HOME/f2 ]
echo $?
Sample outputs:
0
$? is the return code of the last executed command.
Example 2:
$ ls
$ touch file_1
$ dd if=/dev/zero of=$HOME/file_2 bs=1 count=100
100+0 records in
100+0 records out
$ ls -l file_1 file_2 file_3
ls: file_3: No such file or directory
-rw-rw-r-- 1 foo bar 0 Nov 20 20:28 file_1
-rw-rw-r-- 1 foo bar 100 Nov 20 20:28 file_2
$ [[ -s file_1 ]]
$ echo $?
1
$ [[ -s file_2 ]]
$ echo $?
0
$ [[ -s file_3 ]]
$ echo $?
1