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

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?

ADVERTISEMENTS

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • Prashant Aug 27, 2007 @ 9:19

    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

  • Prashant Sep 1, 2007 @ 5:17

    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.

  • nana Dec 10, 2007 @ 10:43

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.