How to find empty folders on Linux

Posted by Planet Malaysia on October 17, 2008

advertisement

Tips: How to find an empty folders on Linux box

In order to do some folder maintenance job, you may require to find an empty folder on Linux box. I would like to share tips here:

# find /path -type d -empty

Find empty folder and list
# find /path -type d -empty -exec ls -ld {} \;

Find empty folder and save as temporary file
# find /path -type d -empty -exec ls -ld >> /tmp/savefiles.txt {} \;

Find empty folder and delete
# find /path -type d -empty -exec rm -rf {} \;

Enjoy!

Possibly Related Posts:


Comments

5 Responses to “How to find empty folders on Linux”

  1. waike on October 23rd, 2008 3:17 pm

    you can easily found tons of similar tutorial.

  2. Maverick Prowls on March 1st, 2009 3:33 am

    Thanks for this, just what I was looking for

  3. Upendraya on March 16th, 2009 11:11 am

    I have posted a complete how-to for the linux newbs at LinuxBuzz.net

  4. felipe1982 on November 3rd, 2009 4:28 pm

    You should probably run
    [code]
    find -type d -empty -exec rmdir '{}' \;
    [/code]
    rmdir will only remove an empty directory, this adds some assurance (sanity) to the removal process. ‘{}’ will ensure strange directory names are ‘quoted’ and protected from your shell

  5. Antonio on January 19th, 2010 12:36 am

    > # find /path -type d -empty -exec rm -rf {} \;

    If we do this, we would have to search again for empty folders (those that just got empty by deleting empty folders inside).

    Better try:

    # find /path -depth -type d -empty -exec rm -r {} \;

    “-depth” means “act on contents before the dir (work from the bottom up)”.

Leave a Reply




Planet Malaysia

  • Follow us on Twitter


  • web www.planetmy.com