Logo Background RSS

How do I remove all empty directories?

  • You can use program called cleanlinks. The cleanlinks program searches the directory tree descended from the current directory for symbolic links whose targets do not exist, and removes them. It then removes all empty directories in that directory tree. It was originally created for symbolic links based directories but works with normal directories too.

    For example if you want to remove all empty directories from /tmp directory, type the command:

    $ cd /tmp
    $ cleanlinks

    Please note that cleanlinks command is part of XFree86 project. Another method is to use combination of shell commands in script:

    #/bin/bash
    DIR=”$1″
    [ -d $DIR ] && [ $(ls -l $DIR | wc -l) -eq 1 ] && rmdir $DIR || :

    Save and execute a script:

    $ script.sh dir1

    You can also try out tmpreaper command which recursively searches for and removes files and empty directories which haven’t been accessed for a given number of seconds. Normally, it’s used to clean up directories which are used for temporary holding space, such as “/tmp”. Syntax is as follows:
    tmpreaper TIME-FORMAT DIRS

    Where,

    • TIME-FORMAT : Defines the age threshold for removing files. The TIME-FORMAT should be a number, defaulting to hours, optionally suffixed by one character: d for days, h for hours, m for minutes, or s for seconds.
    • DIRS : Directory name for example /tmp

    For example, remove all files accessed 24h before:

    # tmpreaper 24h /tmp

    Please note that tmpreaper command is not installed by default you may need to install it using apt-get or rpm command.

23 Comments
  1. #1 infoproservices.com
    August 4th, 2009 at 5:42 am

    Maybe if the page has good information or sticks to the topic, there is a possibility that it can have a good page rank. It might not be worth the time, but who knows what might happen?

    Post ReplyPost Reply
  2. #2 infoproservices.com
    August 4th, 2009 at 5:43 am

    I want to delete all the directories under a parent directory without deleting the parent through DOS command line. That case i don’t know how many directories exist under parent.

    Post ReplyPost Reply
  3. #3 infoproservices.com
    August 4th, 2009 at 5:43 am

    The web directories that I submited to are not Yahoo or Google directories, but those small, not that famous ones which exist thousands online.

    Post ReplyPost Reply
  4. #4 Konsole kaufen
    December 30th, 2009 at 12:31 pm

    It is great to know about cleaning the web directories. Thanks for providing this type of valuable information to others in a great manner. It will really proves to be a nice one for others to make it.

    Post ReplyPost Reply
  5. #5 SEO
    January 29th, 2010 at 11:55 am

    Pound the pavement. Go door to door explaining everything. Advertise in the business sections of newspapers and business radio stations.

    Post ReplyPost Reply
  6. #6 SEO
    February 1st, 2010 at 4:07 pm

    You can also try out tmpreaper command which recursively searches for and removes files and empty directories which haven’t been accessed for a given number of seconds. Normally, it’s used to clean up directories which are used for temporary holding space.

    Post ReplyPost Reply
  7. #7 seo
    March 3rd, 2010 at 10:36 am

    Don’t use xargs at all, just use -delete in find (available in findutils for about 2 years now, so in every ‘recent’ distro.

    Post ReplyPost Reply
  8. #8 seo
    March 30th, 2010 at 3:40 pm

    This folder may also contain certain files that allows the external hard drive to function correctly, thus why you can’t delete it. My external hard drive has autorun files and the like on it too.

    Post ReplyPost Reply
  9. #9 seo
    April 1st, 2010 at 11:13 am

    Action mentioned above by Lingerance seems to be a nonstandard extension – GNU find doesn’t know about it.

    Post ReplyPost Reply
  10. #10 seo
    May 4th, 2010 at 8:20 am

    I really enjoyed your post. It’s true that article marketing can really drive traffic to your website if you know how to. It is one of the most profitable form of advertising and what’s more it’s FREE!

    Post ReplyPost Reply
  11. #11 seo
    June 2nd, 2010 at 8:08 am

    The program has a clean, professional interface and does what it is designed to do. My guess is that the negative reviewers tried to use it on an entire hard drive – including system folders – and thus were rushing in where angels fear to tread.

    Post ReplyPost Reply
  12. #12 job interview question
    January 3rd, 2011 at 2:21 pm

    hmmm..thanks but can i got backup of that files.

    Post ReplyPost Reply
  13. #13 Grand Lake condo rental
    April 2nd, 2011 at 4:37 pm

    It’s true that article marketing can really drive traffic to your website if you know how to.

    Post ReplyPost Reply
  14. #14 injection molding
    July 21st, 2011 at 3:14 pm

    Thanks for the help KalaBandar.com, but the directory that I am trying to delete contains other directories as well that contain files. The code that you provided deletes the files in the test directory successfully.

    Post ReplyPost Reply
  15. #15 injection molding
    August 4th, 2011 at 1:24 pm

    It might not be worth the time, but who knows what might happen?it’s used to clean up directories which are used for temporary holding space.

    Post ReplyPost Reply
  16. #16 vanderbilt beach real estate
    September 29th, 2011 at 10:45 pm

    There are people who consider searching for and deleting empty directories are a trivial thing. The thing is, if they have reached the hundreds it can get extremely exhausting.

    Post ReplyPost Reply
  17. #17 professional speaker
    October 1st, 2011 at 10:51 am

    It depends on the choice of shell and operating system. Is the operating system Windows, MacOS, Linux, other? Is the shell bash, csh, korn, .bat, other?

    Post ReplyPost Reply
  18. #18 dantesh
    October 3rd, 2011 at 7:04 am

    hai i am dantesh thanx for sharing views………this is one of good site which give complete details about certain topics,keep posting & updated

    Watch Hindi Movies Onlines

    Post ReplyPost Reply
  19. #19 professional speaker
    November 2nd, 2011 at 11:18 am

    It think it might be different for each bistro, on Ubuntu I press Alt+F2, it brings up the box “Run Application,” I then type in config-editor.

    Post ReplyPost Reply
  20. #20 injection molding
    January 16th, 2012 at 12:22 pm

    The web directories that I submited to are not Yahoo or Google directories, but those small, not that famous ones which exist thousands online.

    Post ReplyPost Reply
  21. #21 rugged lcd monitors
    January 18th, 2012 at 12:08 pm

    if you want to remove all empty directories from /tmp directory, type the command. thanks….

    Post ReplyPost Reply
  22. #22 rugged lcd monitors
    February 1st, 2012 at 2:13 pm

    I want to delete all the directories under a parent directory without deleting the parent through DOS command line.

    Post ReplyPost Reply
  23. #23 vending machine companies
    February 1st, 2012 at 4:14 pm

    The post described how easily command will use and work.A must read post for a newbie.How have less knowledge about.

    Post ReplyPost Reply
Leave a Comment