How do I delete a directory in Linux? This is a common question asked by Linux newbies. As you start working with the Linux operating system, it’s inevitable that you’ll want to delete a directory at some point.
In this article, we’re going to discuss two methods you can use to delete a directory in Linux. We’ll also provide an example of each of these methods so you can get to work deleting directories as soon as possible.
Deleting Directories in Linux
You may be accustomed to dragging folders into your computer’s trash can when you’re ready to delete them. The trash can acts as a storage facility for documents you are going to delete. Before the file is actually deleted, you need to empty out your trash can.
When you’re using the Linux command line, you should know there is no trash can. Once you’ve deleted a file or a directory, it’s gone. You will not be able to recover it by going into your trash can.
In Linux, there are two ways to delete a directory:
- rmdir: This command deletes an empty directory.
- rm command: This command deletes a directory including its subdirectories.
Let’s walk through a few examples to illustrate how these commands work.
Delete a Directory Using rm
rm command allows you to delete files and directories. This command can be used to delete both empty and non-empty directories, unlike
rmdir as we’ll discuss in a minute.
The syntax for this command is as follows:
rm [flags] [file/folder name]
There are two flags which allow you to delete a directory using
rm. These are:
- -d: Delete an empty directory
- -r: Delete a non-empty directory and all of its contents
Suppose we want to delete a folder called “lib” in our current working directory. If we run the following command we can see what contents our folder contains:
Our command returned:
Because this folder contains files, we need to use the
-r flag to delete it. We can do so using the following command:
rm -r lib/
Our command deleted the “lib” folder and all of its directories.
You can also use
rm to delete multiple directories. To do so, you can specify multiple folder names after
rm. Suppose you want to delete the folders “lib” and “templates” in your current working directory. You could do so using this command:
rm -r lib templates
Force Delete a Directory and Its Contents
By default, the
rm -r command will prompt you to confirm the deletion of a file or folder if either one has been protected from being written. If you want to override this, you can specify the -f flag, like so:
rm -rf lib
This command will delete all directories and subdirectories in the “lib” directory permanently. You will not be prompted to confirm whether you want to delete any files when you run this command, so you should use it sparingly and only when you’re absolutely sure you want to delete the contents of a folder.
Delete a Directory Using rmdir
The rmdir command allows you to delete empty directories. This command is useful if you know a folder is empty and want to remove it from your system.
Suppose our current working directory contains the following files and folders (which we can see by using the “ls” command):
app.py config dev.py
The “config” folder is empty. Suppose you want to delete the folder “config” in your current working directory. You could do so using this command:
Now, if we run
ls to see the contents of our current folder, the following is returned:
You can see that the “config” directory has been deleted. It’s important to note that you cannot use rmdir to delete a directory that contains a file. If our “config” directory contained a file, the following error would have been returned:
rmdir: config/: Directory not empty
rmdir, you can delete directories in Linux.
rm allows you to delete non-empty directories, whereas
rmdir only allows you to delete empty directories.
If you want to learn more about these commands, type in
man, followed by the name of the command, in your terminal. This will show you the Linux manual page for the command.
Before you delete a file, make sure you have chosen the right one to delete. There is no turning back when you use a command like