How to delete a directory in Linux

There are many ways in which you can remove a directory in Linux, here we'll talk about how to do that on a Linux terminal, we all know how easy it is to do it on a GUI system so we won't even mention it.

This tutorial will take you through various ways in which you can delete a directory in Linux.

Delete a directory using "rmdir" command

The rmdir command, short for'remove directory', is a command-line tool that is used to delete empty directories. The operation will be successful if and only if the directory is empty. The syntax for deleting a directory is as follows:

$  rmdir dir_name


If for whatever reason the directory contains files then it will fail with error:

 rmdirdir_name: Directory not empty


Now, there is another way to delete folders and is with the classic "rm" command, it is typically used to delete files, etc, but we can add some arguments to also get rid of folders:

$  rm -r dir_name

This should take care of deleting the folder, it will ask you to confirm and proceed, if it contains files it will also prompt you to confirm deleting those prior to deleting the folder. There is a way to avoid all that confirmation by using the -f option (force):

$ rmdir -rf dir_name

Now, careful as this assumes you know what you're doing, if you enter this command and delete a folder you did not mean to it will be too late (unless you have backups, which you should!)

If you wish to delete multiple directories at once you can use the following command:

$ rmdir -rf dir_name1 dir_name2 dir_name3

 

That's it, you now know how to delete directories in Linux, please note that the rm command can also be used for regular files (and -rf applies there too).

 

If you're looking for a Cloud VPS this is the right place, please check our plans at https://owned-networks.net/kvm.html available at 8 Global Locations (In US and Europe)

  • delete directory, linux
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

ownCloud Auto Install Script on Debian 7

Here is an easy way to install ownCloud on Debian 7 with a script, but first, for those who...

NGINX – Allow access only to certain IPs

Nginx has a nice module that not many people know about, it basically enables us...

How to configure NTP client in CentOS

What's NTP? NTP stands for Network Time Protocol, and it is an Internet protocol used to...

Initial Server Setup on CentOS 6

Here are some recommendations to setup your VPS or server when you first get it, some of these...

How To Install nginx on CentOS

What is NGINX? Nginx (pronounced "engine-x") is an open source reverse proxy server for HTTP,...