How to Delete a Directory in Linux

To delete or remove a directory in Linux, use the “-r” flag to the “rm” command. “r” stands for recursive.

rm -r <directory_name>

On some Linux systems, you might be prompted before each file is deleted. This means that the rm command has been aliased to “rm -i”, which turns on interactive mode. To run the original rm command and avoid the prompts, use "\rm":

\rm -r <directory_name>

In the bash shell, just as the backslash is used to escape special characters, it is also used to escape aliases.

Caution!

Be careful to double check the directory name you provide, and be extra careful when using wildcards. There have been many stories of grief because of users ending up deleting important files and folders recursively.



Comments

There is also the dedicated 'rmdir' command for removing directories.

The directory has to be empty.

It's far easier to get in the habit of overcoming those prompts with:

rm -rf

Both are just as destructive, but this way is more friendly in your bash history.

By Erick

check out 'rmdir' too.

By AC

if I am going to rm -f anything, I always run the command with ls first. I find this especially useful when using regular expressions or wildcards. ls ~/images/[dump,tmp,temp]/*.[gif,jpg,jpeg,png] is much different if there is an accidental space after that star.

I always "rm -rf dirname" this will remove directory without any questions and confirmations. Be careful with that.

By Polk

The other option to avoid prompts with *any* command is to pipe the output of the 'yes' command to their standard input like this:

yes | rm -r directory

By David

from rm command description
--------------------------------------
If the removal of a non-empty, write-protected directory is attempted, the utility will always fail (even if the -f option is used), resulting in an error message.

So , u r worried about ramifications of rm -f , make sure u have imp directories /files write protected (permissions like 555 )

By Vishal Chougule

i tried to delete a folder having some photos(jpg), but its showing pemission denied.
i also tried to delete the folder using shift+delete buts its not working. Need help from my expert friends...
plz.

By deepan

What is permission of your directory ? What is permission of files you are trying to delete ?

By Vishal

Hi Deepan,

Could you check the permission of the folder you're trying to delete?

You could check it using the "ls -l" command, or by right clicking and checking out the folder properties.

Next, you'd want to change the permissions so that you'd be able to delete the folder. Alternatively, you could try running the following command (assuming you're running Ubuntu or a similar distro): "sudo rm -r folder_name". Just be careful when running that command, since there's no going back if you delete the wrong folder.

Hope that helps - do get back if you're stuck again!

By Abdullah Chougle

Add new comment