Skip to main content
Quick Tip

Find Empty Directories in Linux Command Line

Here are a few examples of finding and deleting empty directories in the Linux command line.

Sagar Sharma

Warp Terminal

Linux users are bound to have multiple empty directories and it's quite easy to find them.

But wait, just listing empty directories won't do the job so I'll also show you how to delete them!

Find Empty Directories in Linux

To Find an empty directory, you'll just have to pair the -empty option with the find command. Let me show you how:

find /home/sagar/Files -type d -empty

When you use the find command with the -type d option, it will search for directories only.

Find Empty Files in Linux

As I mentioned just above that -type d was used to search for directories, you can change this behavior to search for files  by using type f.

find /home/sagar/Files -type f -empty

Find is an excellent command. If you are interested in learning more, here are a few more examples 👇

15 Super Useful Examples of Find Command in Linux
Learn the super powerful and super useful find command with these practical examples.
⚠️
Don't go blindly running the delete command, please. Be careful about what you delete.

Delete Empty Files and Directories using Find Command

To delete the files and directories given by the find command, you just have to use the -delete option.

So let's start with deleting empty directories:

find /home/sagar/Files -empty -type d -delete

You can do the same with files by interchanging -type d with -type f:

find /home/sagar/Files -empty -type f -delete

Delete Empty Files and Directories using xargs and find-exec

The find exec is nothing but allows you to execute custom operations such as running scripts and executing programs on search results.

While xargs can take input from the standard input or even consider the output of another command as input and can use it as a command.

So let's start with removing empty files and directories using the find with the exec command.

To remove empty files, you just have to utilize the given command:

find /home/sagar/Files -type f -empty -print0 -exec rm -v "{}" \;

And if you want to remove directories, the given command should work just fine:

find /home/sagar/Files -type d -empty -print0 -exec rmdir -v "{}" \;

Now, let's use xargs with find to remove empty directories:

find /home/sagar/Files -type d -empty -print0 | xargs -0 -I {} /bin/rmdir "{}"

Similarly, with a few tweaks to the above command and you can remove empty files:

find /home/sagar/Files -type f -empty -print0 | xargs -0 -I {} /bin/rm "{}"

And if you're curious to learn more about xargs, we already have a detailed guide on that topic:

How to Use Xargs Command in Linux [Explained With Examples]
xargs is one of the most powerful commands in Linux. In this tutorial, you’ll learn to use xargs command with some practical and useful examples.

Final Words

Well, this was a short guide on how you can remove empty files and folders and I've tried my best to make it beginner friendly.

But still, if you encounter any doubts, I'd be happy to help!

Sagar Sharma