How to check if a file is empty in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a file is empty in Linux using different command-line tools. You will explore three common methods: using the test -s command to check file size, employing the wc -c command to count the number of bytes in a file, and finally, examining the file's contents directly with the cat command. Through hands-on examples, you will understand how each method works and how to interpret their output to determine if a file contains data.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicSystemCommandsGroup -.-> linux/test("Condition Testing") linux/BasicFileOperationsGroup -.-> linux/touch("File Creating/Updating") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/BasicFileOperationsGroup -.-> linux/wc("Text Counting") subgraph Lab Skills linux/echo -.-> lab-558711{{"How to check if a file is empty in Linux"}} linux/test -.-> lab-558711{{"How to check if a file is empty in Linux"}} linux/touch -.-> lab-558711{{"How to check if a file is empty in Linux"}} linux/cat -.-> lab-558711{{"How to check if a file is empty in Linux"}} linux/wc -.-> lab-558711{{"How to check if a file is empty in Linux"}} end

Check file size with test -s

In this step, you will learn how to check if a file exists and is not empty using the test command with the -s option. The test command is a built-in shell command used to evaluate conditional expressions. It's often used in shell scripts to make decisions.

The -s option with test checks if a file exists and has a size greater than zero (i.e., it's not empty).

First, let's create an empty file and a non-empty file in your ~/project directory.

Open your terminal if it's not already open. You should be in the ~/project directory by default.

Create an empty file named empty_file.txt:

touch empty_file.txt

Now, create a file named non_empty_file.txt and add some text to it using the echo command and the redirection operator >. The > operator redirects the output of a command to a file. If the file doesn't exist, it's created. If it exists, its contents are overwritten.

echo "This file has content." > non_empty_file.txt

Now, let's use the test -s command to check these files. The test command doesn't produce output on success. It signals success or failure using its exit status. An exit status of 0 means success (the condition is true), and a non-zero exit status means failure (the condition is false).

We can check the exit status of the previous command using the special variable $?.

Check the empty file:

test -s empty_file.txt
echo $?

You should see the output 1, indicating that the condition (file exists and is not empty) is false for empty_file.txt.

Now, check the non-empty file:

test -s non_empty_file.txt
echo $?

You should see the output 0, indicating that the condition is true for non_empty_file.txt.

This is a fundamental way to check file properties in shell scripting. You can use this to ensure a file has content before attempting to process it.

Click Continue to proceed.

Count file bytes using wc -c

In this step, you will learn how to count the number of bytes in a file using the wc command with the -c option. The wc command is a utility that prints newline, word, and byte counts for each file.

The -c option tells wc to only count the bytes.

Let's use the files we created in the previous step: empty_file.txt and non_empty_file.txt.

First, count the bytes in empty_file.txt. Make sure you are in the ~/project directory.

wc -c empty_file.txt

You should see output similar to this:

0 empty_file.txt

This shows that empty_file.txt has 0 bytes, which makes sense because it's an empty file.

Now, count the bytes in non_empty_file.txt:

wc -c non_empty_file.txt

You should see output similar to this:

23 non_empty_file.txt

The number 23 represents the number of bytes in the file "This file has content." followed by a newline character added by echo. Each character typically takes up one byte.

The wc command is very useful for quickly getting information about the size of files. You can also use wc without options to get line, word, and byte counts. Try running wc non_empty_file.txt to see the difference.

Click Continue to move to the next step.

View file contents with cat command

In this step, you will learn how to view the contents of a file using the cat command. The cat command is short for "concatenate" and is primarily used to display the content of files.

Let's use the files we created in the previous steps: empty_file.txt and non_empty_file.txt. Make sure you are in the ~/project directory.

First, try to view the content of empty_file.txt:

cat empty_file.txt

Since empty_file.txt is empty, the cat command will not display any output.

Now, view the content of non_empty_file.txt:

cat non_empty_file.txt

You should see the content we added in the previous step:

This file has content.

The cat command is a simple and quick way to display the entire content of a file directly in your terminal. Be cautious when using cat on very large files, as it will print everything to your screen, which can be overwhelming. For large files, commands like less or more are more suitable as they allow you to view the content page by page.

You can also use cat to combine the contents of multiple files and display them. For example, if you had file1.txt and file2.txt, cat file1.txt file2.txt would display the content of file1.txt followed by the content of file2.txt.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if a file is empty in Linux using the test -s command. This command checks if a file exists and has a size greater than zero, returning an exit status of 0 for true (not empty) and 1 for false (empty or non-existent). You practiced creating empty and non-empty files and verifying their status using test -s and checking the exit status with $?.