Removing large directories in Linux

Ever try to delete the contents of a large directory in nix type operating system? If you have many files you will get an error like “list to long” or something like that. This happens to some of my directories I use to cache pages, sometimes I want to clear them out manually.

List too long

So now what?

The easiest and quickest solution is to remove the entire directory with a command like this.

(Now, before you do it, you need to realize that you will have to recreate the directory when it’s done, so take note of the permissions you have on the directory first.)

rm -rf directoryname

rm -rf

You can see the directory is now gone “No such file or directory”.

Yes, that is exactly what I wanted!

This entry was posted in other stuff and tagged , , . Bookmark the permalink.

3 Responses to Removing large directories in Linux

  1. Mitre Box says:

    Thanks.

    Saved a couple of hours.

  2. loader says:

    In case you want remove just htm files…

    find . -type f -name “*.htm” -exec rm {} \;

    • Matz says:

      This isn’t a very effective solution:
      find . -type f -name “*.htm” -exec rm {} \;
      For a directory with 20000 files, 20000 times shell will execute rm and this will use a lot of CPU and IO time.
      A much more efficient solution for directories with many files is:
      find . -type f -name “*.htm” | xargs -l500 rm -f
      This will execute rm for every 500 files, causing much less hassle for the system.
      In general, i think using xargs is a whole lot better than -exec in find, and you’d best avoid it when possible.

      It may also be used in combination with grep, to clean a folder out of files containing a specific string a-la:
      grep -rF 'some string in files' . | xargs -l500 rm

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>