≡ Menu

recursively delete files

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.