EN
Bash - check if file is not zero size
0
points
In this article, we would like to show you how to check if file is not zero size in Bash.
Practical example
In this example, we use -s operator with specified path to check if file under the path is not zero size inside if statement.
script.sh file:
#!/bin/bash
path="/path/to/not/empty/file.txt"
if [ -s "$path" ]; then
echo "The file is not zero size."
fi
Note: this approach should be used in POSIX shells where other solutions may not work.
Run in command line:
./script.sh
Output:
The file is not zero size.
Altermative notation
1. test based solution
#!/bin/bash
path="/path/to/not/empty/file.txt"
if test -s "$path"; then
echo "The file is not zero size."
fi
Note:
[ ]notation is shorthand oftestcommand.
2. [[ ]] based solution
#!/bin/bash
path="/path/to/not/empty/file.txt"
if [[ -s "$path" ]]; then
echo "The file is not zero size."
fi
Note:
[[ ]]notation is like extendedtestcommand, adding additional features (e.g. regular expressions).