Linux Delete Folder Recursively

by on December 15, 2008 · 3 comments· LAST UPDATED March 18, 2014

in , ,

How do I delete folder recursively under Linux operating systems using a bash command line options?

You need to use the rm command to remove files or directories (also known as folders) recursively. The rmdir command removes only empty directories. So you need to use rm command.

rm command syntax to delete directories recursively

The syntax is as follows:

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time2m

rm -r dirName

OR

rm -r folderName

OR

rm -rf folderName

Examples

In this example, recursively delete data folder:

 
rm -r /home/vivek/data/
 

The specified /home/vivek/data/ will first be emptied of any subdirectories including their subdirectories and files and then data directory removed. The user is prompted for removal of any write-protected files in the directories unless the -f (force) option is given on command line:

 
rm -rf dirname-here
 

OR

 
rm -r -f /path/to/folder/
 

To remove a folder whose name starts with a -, for example '--dsaatia', use one of these commands:

 
rm -rf -- --dsaatia
 

OR

 
rm -rf ./--dsaatia
 

See Linux rm(1) command man page or rm command example page for more information.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 Thota Madhu Sudhan March 11, 2013 at 6:11 pm

What is the significance of -r in the code ?
rm -r dirname
what is the difference in using -rf and -r ?

Reply

2 K-hole March 18, 2014 at 2:34 pm
  • -r :recursive
  • -f :force (no prompt)

Reply

3 tuunga July 9, 2013 at 1:42 pm

# rm -r dirName
on centos ask every file to be deleted

Reply

Leave a Comment

Tagged as: , , , , , , , , , ,

Previous Faq:

Next Faq: