In Linux, an empty file is a file that has a size of zero bytes. This means that the file does not contain any data, and it does not have any content when it is opened in a text editor. An empty file can be created using the touch command: `touch myfile.txt`
This will create an empty file called myfile.txt in the current directory. You can also create an empty file using the echo command with the > operator: `echo > myfile.txt`
This will overwrite any existing content in the file with an empty string, effectively creating an empty file.
Method to check if a file is empty in Bash
There are several different ways to check if a file is empty in Bash, depending on your specific needs and the tools that are available on your system. In this article, we’ll look at five different approaches for checking if a file is empty in Bash.
- Using the
`test`
command or`[`
The
`test`
command (which is an alias for the`[
` command) is a simple and widely-available utility for performing various tests on files and other objects in Bash. To check if a file is empty using`[`
, you can use the`-s`
option, which checks the size of the file:12345678910#!/usr/bin/env bashFILENAME=myfile.txt# Check if the file is emptyif [ ! -s "${FILENAME}" ]; thenecho "File is empty"elseecho "File is not empty"fiNote that the
`-s`
option considers a file to be empty if its size is zero, but it does not check for the presence of hidden characters or whitespace. If you want to check for a completely empty file (i.e., one that contains only whitespace or has no lines at all), you can use the`-z`
option, which checks if the string is empty:123456789#!/usr/bin/env bashFILENAME=myfile.txt[ -z "$(cat ${FILENAME})" ] thenecho "File is empty"elseecho "File is not empty"fiThis will read the contents of the file and pass them to the test command as a string. If the string is empty, the file is considered empty.
You can also check for files existing before testing for empty file. The below script will check if the file exists and if the file is empty or not.
12345678910111213#!/usr/bin/env bashFILENAME=myfile.txtif [ -f "${FILENAME}" ];thenif [ -s "${FILENAME}" ];thenecho "File ${FILENAME} exists and not empty"elseecho "File ${FILENAME} exists but empty"fielseecho "File ${FILENAME} not exists"fiAs per the above example, if myfile.txt does not exist, the script will show the output as “File not exists” and if the file exists and is an empty file then it will show the output as “File exists but empty”, else if file exists has some content in it will show output as “File exists and not empty”.
- Using the wc command
The `wc` (word count) command is another utility that can be used to check if a file is empty in Bash. To check the number of lines in a file, you can use the
`-l`
option:12345678910#!/usr/bin/env bashFILENAME=myfile.txt# Check if file has any linesif [ $(wc -l < "${FILENAME}") -eq 0 ]; thenecho "File $FILENAME is empty"elseecho "File $FILENAME is not empty"fiThis will pass the contents of the file to the `wc` command as standard input, and the
`-l`
option will count the number of lines. If the number of lines is zero, the file is considered empty. - Using the grep command
The `grep` command is a powerful tool for searching and processing text files. To check if a file is empty using grep, you can use the
`-q`
option, which causes grep to run quietly and return an exit code:123456789#!/usr/bin/env bashFILENAME=myfile.txtif grep -q . "${FILENAME}"; thenecho "File is not empty"elseecho "File is empty"fiThis will search the file for any character (. is a regular expression that matches any character) and return a zero exit code if a match is found, or a non-zero exit code if the file is empty.
Using the find command
The `find` command is a utility for searching and processing files and directories. To check if a file is empty using find, you can use the -empty option, which matches files that are empty:
1 2 3 4 5 6 7 8 | #!/usr/bin/env bash FILENAME=myfile.txt if find . -type f -empty -name "${FILENAME}"; then echo "File is empty" else echo "File is not empty" fi |
This will search the current directory (.) for files (-type f) that are empty (-empty) and have the name “myfile.txt” (-name “myfile.txt”). If a match is found, the find command will return a zero exit code, indicating that the file is empty. If no match is found, the find command will return a non-zero exit code, indicating that the file is not empty.
Using the stat command
The `stat` command is a utility for displaying information about files and filesystems. To check if a file is empty using stat, you can use the -c option to display the size of the file in bytes:
1 2 3 4 5 6 7 8 | #!/usr/bin/env bash FILENAME=myfile.txt if [ $(stat -c %s "${FILENAME}") -eq 0 ]; then echo "File is empty" else echo "File is not empty" fi |
This will pass the size of the file to the `[` command, which will compare it to zero. If the size is zero, the file is considered empty. If the size is non-zero, the file is considered not empty.
I hope these examples have given you a good understanding of the different ways to check if a file is empty in Bash. As you can see, there are many different approaches to choose from, and the best one will depend on your specific needs and the tools that are available on your system.
3 Comments
Rahul, great, short and sweet. Another website had a more involved approach to this, I always like the clear and concise approach.
what if file name variable is empty ?? Still it says File exists and not empty
#!/bin/bash
FILENAME=””
if [ -f ${FILENAME} ];then
if [ -s ${FILENAME} ]
then
echo “File exists and not empty”
else
echo “File exists but empty”
fi
else
echo “File not exists”
fi
Thanks Rahul, great post