Bash

How to check if a file is empty in Bash

27 September 2026 · 4 min read

How to check if a file is empty in Bash

Dealing with files is a fundamental aspect of working with any operating system, and Bash, the ubiquitous Unix shell, offers a wealth of tools for file manipulation. One common task is determining whether a file is empty. Knowing how to efficiently check for empty files can be crucial for automating tasks, scripting, and ensuring data integrity. This post will delve into various methods for checking if a file is empty in Bash, providing clear explanations, practical examples, and best practices.

Using the -s Test Operator

The -s operator is arguably the most straightforward way to check for empty files. This operator checks if a file exists and has a size greater than zero. If the file is empty or doesn’t exist, the test evaluates to false.

Here’s a simple example:

if [ -s "file.txt" ]; then echo "File is not empty" else echo "File is empty or does not exist" fi 

This method is efficient and easily readable, making it a popular choice for many scripting scenarios.

Leveraging the stat Command

The stat command provides detailed information about a file, including its size. We can extract the size information and use it to determine if a file is empty.

Example:

if [[ $(stat -c %s "file.txt") -eq 0 ]]; then echo "File is empty" else echo "File is not empty" fi 

This approach allows for more granular control, as stat offers a variety of options for retrieving file information.

Employing the wc Command

The wc command (word count) is primarily used for counting lines, words, and bytes in a file. We can utilize the -c option to count bytes and check if the count is zero, indicating an empty file.

Example:

if [[ $(wc -c < "file.txt") -eq 0 ]]; then echo "File is empty" else echo "File is not empty" fi 

While effective, using wc involves reading the entire file, which can be less efficient for very large files compared to the -s operator.

Reading the Entire File

For smaller files, reading the entire contents into a variable and checking its length is a viable option. This approach allows you to verify the file’s emptiness and potentially perform other operations on its content.

Example:

content=$(<"file.txt") if [[ -z "$content" ]]; then echo "File is empty" else echo "File is not empty" fi 

This method is generally less efficient for large files due to the overhead of reading the entire file into memory. Be mindful of memory usage when applying this technique to potentially large files.

  • The -s operator is the most efficient for simply checking if a file is empty.
  • The stat command offers flexibility for retrieving other file information.
  1. Choose the method that best suits your specific needs and file sizes.
  2. Consider the potential performance impact when dealing with very large files.
  3. Always test your scripts thoroughly to ensure they behave as expected.

For further reading on Bash scripting, refer to the GNU Bash Manual.

Infographic Placeholder: (Visual representation of the different methods and their efficiency)

It’s crucial to understand the nuances of each method for checking file emptiness in Bash. Selecting the right approach can significantly impact the performance and efficiency of your scripts. By leveraging the tools discussed, you can streamline your file-handling operations and ensure data accuracy. Remember to always consider file sizes and potential performance implications when choosing the most appropriate technique. Explore the provided resources for further learning and delve deeper into the world of Bash scripting. For a deeper dive into file management, check out this tutorial on advanced file manipulation techniques. You might also find helpful information on file handling from stat command documentation and wc command examples. This knowledge will empower you to create more robust and efficient scripts for managing files effectively within a Bash environment.

  • Regularly check for empty files to maintain data integrity.
  • Automate file-handling tasks using Bash scripts for increased efficiency.

FAQ

Q: What’s the fastest way to check if a file is empty?

A: The -s operator is generally the most efficient for simply checking file emptiness.

Q: How do I handle potential errors when checking for empty files?

A: Implement error handling mechanisms within your script, such as using the || operator to handle cases where the file doesn’t exist.

Question & Answer :
I have a file called diff.txt. I want to check whether it is empty.

I wrote a bash script something like below, but I couldn’t get it work.

if [ -s diff.txt ] then touch empty.txt rm full.txt else touch full.txt rm empty.txt fi 

try this:

#!/bin/bash -e if [ -s diff.txt ]; then # The file is not-empty. rm -f empty.txt touch full.txt else # The file is empty. rm -f full.txt touch empty.txt fi 

Notice incidentally, that I have swapped the roles of empty.txt and full.txt, as @Matthias suggests.