How to check if a directory is empty in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a directory is empty in Linux using various command-line tools. You will begin by using the fundamental ls command with the -a option to list all directory contents, including hidden files, and understand the significance of entries like . and ...

Next, you will explore the find command to recursively search for files within a directory, providing another method to check for the presence of content. Finally, you will learn how to use the du -sh command to verify the size of a directory, offering a quick way to confirm if it contains any data. By completing these steps, you will gain practical skills for efficiently checking directory emptiness in a Linux environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/touch("File Creating/Updating") linux/FileandDirectoryManagementGroup -.-> linux/find("File Searching") linux/SystemInformationandMonitoringGroup -.-> linux/du("File Space Estimating") subgraph Lab Skills linux/ls -.-> lab-558706{{"How to check if a directory is empty in Linux"}} linux/touch -.-> lab-558706{{"How to check if a directory is empty in Linux"}} linux/find -.-> lab-558706{{"How to check if a directory is empty in Linux"}} linux/du -.-> lab-558706{{"How to check if a directory is empty in Linux"}} end

List directory contents with ls -a

In this step, you'll learn how to list the contents of a directory using the ls command. The ls command is one of the most fundamental commands in Linux, used to list files and directories.

By default, ls shows you the visible files and directories in your current location. Your current location is the directory you are currently working in. When you open the terminal in this lab, your current location is /home/labex/project.

Let's try the basic ls command. Type the following in your terminal and press Enter:

ls

You might see some output, or perhaps nothing if the directory is empty.

Now, let's explore a useful option for the ls command: -a. The -a option stands for "all" and tells ls to show all files, including hidden ones. In Linux, files and directories that start with a dot (.) are considered hidden. Configuration files are often hidden this way.

Type the following command and press Enter:

ls -a

You will likely see more entries than before, including entries like . and ...

.
..
.zsh_history
  • . represents the current directory.
  • .. represents the parent directory (the directory one level up).
  • .zsh_history is a hidden file that stores your command history.

Understanding hidden files is important as they often contain configuration settings for applications and the system.

Practice using ls and ls -a in your ~/project directory. See the difference in the output.

Click Continue to proceed to the next step.

Check for files with find command

In this step, you'll learn how to use the find command to search for files and directories within a specified location. While ls lists contents of a single directory, find can search recursively through subdirectories.

The basic syntax for find is:

find [starting_point] [expression]
  • [starting_point] is the directory where the search begins. . means the current directory.
  • [expression] specifies what you are looking for (e.g., file name, type, size).

Let's create a simple file to search for. We'll use the touch command, which creates an empty file. Make sure you are in the ~/project directory.

touch my_document.txt

Now, let's use find to locate the file we just created. We'll start the search in the current directory (.) and look for a file named my_document.txt.

find . -name my_document.txt

The -name option tells find to search for entries with a specific name.

You should see the path to the file as the output:

./my_document.txt

This confirms that find successfully located the file.

The find command is very powerful and has many options for searching based on different criteria like file type (-type f for file, -type d for directory), size, modification time, and permissions.

For example, to find all directories within the current directory and its subdirectories, you could use:

find . -type d

This would list all directories, including . and .. and any subdirectories you might have created.

Experiment with find in your ~/project directory. Try creating another file or directory and then using find to locate it.

Click Continue to move on.

Verify directory size with du -sh

In this step, you'll learn how to check the disk usage of files and directories using the du command. This is useful for understanding how much space your files are taking up.

The du command stands for "disk usage". By default, du shows the disk usage of each file and subdirectory within a given directory, in blocks. This output can be quite detailed.

Let's try the basic du command in your ~/project directory:

du

You'll see output showing the size of my_document.txt (which is likely 0 since it's empty) and the total size of the current directory.

To get a more human-readable output, we can use options with du. Two common options are:

  • -s: Summarize. This option displays only the total size for the specified directory, rather than listing every file and subdirectory.
  • -h: Human-readable. This option displays sizes in a format that is easier for humans to read (e.g., KB, MB, GB).

Let's combine these options to get a summary of the disk usage of your ~/project directory in a human-readable format.

du -sh .

The . at the end specifies that we want to check the disk usage of the current directory.

The output will look something like this:

4.0K    .

This indicates that the current directory (.) is taking up approximately 4.0 Kilobytes of space. The exact size might vary slightly depending on the system and the number of hidden files.

The du -sh command is a quick way to see the total size of a directory. You can also specify a different directory path instead of . to check the size of other directories.

For example, to check the size of your home directory (~), you could use:

du -sh ~

Practice using du -sh on different directories if you have created any others in this lab.

Click Continue to complete this step and the lab.

Summary

In this lab, you learned how to check if a directory is empty in Linux using several fundamental commands. You started by using ls -a to list all contents, including hidden files and the special entries . (current directory) and .. (parent directory), which are always present.

You then began exploring the find command, a powerful tool for searching for files and directories recursively within a specified starting point.