You can delete a directory in Linux using the rm command. The rm command can delete a directory if it contains files as long as you use the -r flag. If a directory is empty, you can delete it using the rm or rmdir commands.
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.
How to Delete a Directory in Linux
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. You must use the -r flag with this command if the folder contains any files.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
You may be accustomed to dragging folders into your computer’s trash can when you’re ready to delete them. The trash acts as a storage facility for documents you are going to delete. Before the file is 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.
To learn more about the command line, read our guide to learn the Linux command line.
Linux Delete Directory: rm Command
The 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 the rm 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 returns:
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. This happens if a file you are trying to delete has been protected. 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 this command sparingly and only when you’re absolutely sure you want to delete the contents of a folder.
Delete Directory Linux: rmdir Command
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 Linux “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:
If we run ls to see the contents of our current folder, our command returns:
You can see that the “config” directory no longer exists. 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
Using rm and rmdir, you can delete directories in Linux. rm deletes non-empty directories. The rmdir command delete empty directories. It cannot be used to delete a directory that contains any files.
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 rm or rmdir.
To learn more about Linux, read our How to Learn Linux guide. This guide contains courses and resources to help you along your journey toward learning Linux.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.