About Linux FAQ

Browse More FAQs:

How to delete lots of directory at a one time in Linux or UNIX

Posted by Vivek Gite [Last updated: August 17, 2007]

Q. I’d like to delete lots of directory at a one time in Linux or UNIX. For example /data2/2005 is old archived data and it is no longer needed. How do I delete all directories and files from /data2/2005? Do I need to write a script?

A.. You need to use rm command to remove files or directories at a once time. You don't have to write a script. Modern rm command has -r option which remove directories and their contents recursively. You also need to pass -f (force) option. In short enter command as follows to wipe out everything inside /data2/2005 directory:
$ rm -rf /data2/2005

However if you have thousands and thousands of files and sub directories, rm may not work out of box due to shell limitation. Try the combination of find and rm command as follows:
$ find -t f /data2/2005 -exec rm -rf '{}' \;
$ find /data2/2005 -exec rm -rf '{}' \;

The first one will only wipe out files.

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Prashant Says:

    How do I delete files which have been generated before perticular date. I would like to delete all files which are generated on or before 31.12.2006

  2. Prashant Says:

    I have created printers on Linux server for different X11 clients. Is it possible to copy all these printer configuration on another server as we are going to up another server.

  3. nana Says:

    how can we make it through the day??? without you…Say you’ll never go…hehehe

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.