≡ Menu

How to Find and Delete Empty Directories and Files in Unix

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.

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… add one }

  • Tester March 12, 2010, 10:44 am

    Very good way to delete it.

  • Tester2 March 14, 2010, 4:57 pm

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

  • jameslee March 16, 2010, 8:26 pm

    really nice..very useful

  • Ramesh June 25, 2012, 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

  • don July 10, 2012, 10:26 am

    thanks for the trick :)

  • DEC.I August 14, 2012, 1:10 am

    me love geekstuff :X

  • Wojciech October 10, 2012, 9:05 am

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

  • andy December 27, 2012, 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`
    /bin/rm -r -f $x

  • Alex April 7, 2013, 2:20 am

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

  • Chris May 13, 2013, 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…

  • britPaul June 17, 2013, 10:38 am


    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.

  • Blackjackshellac September 20, 2013, 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 {} \;

  • Blackjackshellac September 20, 2013, 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’

  • Rao April 15, 2014, 12:16 pm

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

Leave a Comment