In this post, we’re going to explore how to find and deal with empty files and folders in Linux, and we’ll keep it simple.
Whether you’re new to Linux or just want to clean up your computer, this guide will help you easily locate and manage empty files and folders. We’ll take you through the basics step by step, so you can enjoy a more organized and efficient Linux experience.
Table of Contents
Search for Empty Files in the specific Directory and Subdirectories
In Linux, an empty file is a file that contains no data or has a file size of 0 bytes. An empty file is essentially a file with no content; it doesn’t store any information or data.
We can use the following two ways to locate empty files.
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!find /path/to/directory -type f -empty
or
find /path/to/directory -type f -size 0
- find: This is the command used to search for files and directories.
- /path/to/directory: Replace this with the actual path to the directory where you want to start your search. The command will look for empty files in this directory and its subdirectories.
- -type f: This option specifies that you are looking for regular files (not directories or other types of files).
- -empty: This option tells find to search for files with no data in them, essentially empty files.
- -size 0: This option filters files based on their size. Specifically, it looks for files with a size of 0 bytes, which are empty files.
If you want to find empty directories in the current directory, you can use the following command:
find . -type d -empty
or
find . -type d -size 0
Example of locating empty files in Linux
Let’s create an example with three files in the current directory. We’ll use the touch command to create the files:
# Create three files: file1.txt, file2.txt, and file3.txt
touch file1.txt
touch file2.txt
touch file3.txt
# Add data to file1.txt
echo “This is some content for file1.txt” > file1.txt
# Add data to file3.txt
echo “Another example text for file3.txt” > file3.txt
Now, we have three files in the current directory, and file2.txt is empty. To find and list the empty file, you can use the find command as follows:
find . -type f -size 0
When you run this command, it will display the path to the empty file file2.txt as output.
More examples
Find Empty Directories Owned by a Specific User:
find . -type d -empty -user username
Replace username with the name of the specific user. This command lists empty directories owned by the specified user in the current directory and its subdirectories.
Find Empty Files with a Specific Name:
find . -type f -name "example.txt" -empty
This command lists empty files named “example.txt” in the current directory and its subdirectories.
Find Empty Directories and Execute a Command on Each Found Directory:
find . -type d -empty -exec command {} \;
Replace command with the command you want to execute on each found empty directory. The {} placeholder represents the directory name. This command will execute the specified command on each empty directory found.
Remove Empty Directories:
find /path/to/directory -type d -empty -delete
Replace /path/to/directory with the actual directory path where you want to search for and remove empty directories. This command will locate and remove all empty directories within the specified path and its subdirectories.
Remove Empty Files:
find /path/to/directory -type f -empty -delete
These examples demonstrate how you can use the find command with the -empty option along with other options to customize your search for empty files and directories in Linux.