How to Find and Delete Empty Directories and Files in Unix

by Ramesh Natarajan on March 12, 2010

Question: How do I locate empty directories that doesn’t contain any files? Also, how do I find all empty files ( zero byte files ) in Linux?

Answer: You can use unix find command to get a list of all empty files and directories as explained below.

Also, refer to our earlier articles about unix find command examples – part 1 and find command examples – part 2.

Find empty directories in the current directory using find -empty

find . -type d -empty

Use the following command to remove all empty directories under the current directory.

find . -type d -empty -exec rmdir {} \;

Note: It is not recommended to remove empty directories from /etc/ or any other system directories.

Find empty files in the current directory using find -empty

find . -type f -empty

Note: Typically empty files are created by some programs as place holders, or as lock files, or as socket files for communication.

How many empty files are located under the current directory (and sub-directories)?

To count the number of empty files under current directory, pipe the find command to wc -l

find . -type f -empty | wc -l

How many non-empty files are located under the current directory (and sub-directories)?

find . -type f -not -empty | wc -l

Note: Find option -not reverts the option that follows it.

In all the above examples, replace the ( . ) dot with any other directory-path under which you would like to search the files.

Mommy, I found it! — 15 Practical Linux Find Command Examples tutorial explains find command’s maxdepth, mindepth, and invert match.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 14 comments… read them below or add one }

1 Tester March 12, 2010 at 10:44 am

Very good way to delete it.

2 Tester2 March 14, 2010 at 4:57 pm

Why you don’t use the option “-delete” to delete the directories?

3 jameslee March 16, 2010 at 8:26 pm

really nice..very useful

4 Ramesh June 25, 2012 at 8:26 am

Hi folks,
Any one can tell me How to delete all the sudirectory text files from main directory? in UNIX.
I appreciate your valuable answers. Thanks
G.Ramesh

5 don July 10, 2012 at 10:26 am

thanks for the trick :)

6 DEC.I August 14, 2012 at 1:10 am

me love geekstuff :X

7 Wojciech October 10, 2012 at 9:05 am

And how do you delete folders which have just empty folders in them

8 andy December 27, 2012 at 9:59 pm

This is what I do for directories that are completely empty at the first level:

for x in `find . -maxdepth 1 -type d -empty`
do
/bin/rm -r -f $x
done

9 Alex April 7, 2013 at 2:20 am

@Andy:
You could just use
find . -maxdepth 1 -type d -empty -exec rm {} \;
… or you use the option “-delete” as already mentioned above by Tester2.

10 Chris May 13, 2013 at 6:17 pm

How would you best handle the escaping of spaces in the paths?
Something along the lines of:
find . -type d -empty | sed -e ‘s/ /\\ /g’ | xargs rmdir

which doesn’t work for me on OS X…

11 britPaul June 17, 2013 at 10:38 am

@Chris:

The option you’re looking for is -print0, this will null-separate the output from find. Then you use -0 in xargs, like so:

find ./ -type d -empty -print0 | xargs -0 -I{} rmdir “{}”

Of course, using “find . -type d -empty -delete” avoids the problem completely.

12 Blackjackshellac September 20, 2013 at 12:58 pm

Should probably use the -depth argument to delete to descend all the way down the directory tree before removing empty directories,

find . -depth -type d -empty -exec rmdir {} \;

13 Blackjackshellac September 20, 2013 at 1:01 pm

@chris can you use single quotes on the find path spec ‘{}’, this works for me on bash under linux,

# find /tmp -depth -type d -empty -exec rmdir -v ‘{}’ \;
rmdir: removing directory, `./this is a test’

14 Rao April 15, 2014 at 12:16 pm

All,
How to delete NULL file from the multiple files/folders in single go.

Leave a Comment

Previous post:

Next post: