With the information in this article, you will be able to delete a directory in Linux like a pro, including directories containing files and empty ones.
If you are making use of Linux, you can use delete directories using the rm command. The rm command can remove directories containing files if you use the -r flag. If the folder or directory is empty, you can remove it using the rm or rmdir commands.
If you are wondering how to delete a directory or folder in Linux, you should know that this a common issue for Linux newcomers. When you start using a Linux operating system, it’s inevitable that at some point you’ll want to delete a folder.
Using Linux requires more than learning the basics of the C programming language and other programming languages. In this article, we will discuss two methods that can be used to delete folders in Linux. We also provide examples of each method so you can start deleting folders as quickly as possible.
How to Delete a Directory in Linux
There are two ways to delete a directory in Linux:
- rmdir: This is a command used to delete empty directories.
- rm command: This is a command used to delete a folder or directory including its subfolders and sub directories. If the directory contains files, the -r flag must be used with this command.
When you’re ready to delete a folder, you probably tend to drag it to your computer’s trash. The Recycle Bin serves as a place to store documents that you want to delete. Empty the Recycle Bin before the files will be deleted.
When using the Linux command line, note that there is no recycle bin. When you delete a file or directory, you won’t be able to see it again.
Delete Linux Directories Using The rm Command
With the rm command, you can delete directories and files. Unlike rmdir, which we will discuss later, this command can be used to remove both empty and non-empty directories.
Below is what you need to know about the syntax of the rm command:
rm [flags] [file/folder name]
Below are the two flags you can use to remove directories with rm:
- -d: This will delete a directory in Linus that has nothing in it
- -r: This will delete a non-empty directory and everything that is contained in it.
Let’s say we want to remove a folder called “vibes” from the current working directory. We can see what our folder contains if we run the following command:
Our command will bring back something like this:
Since this directory has files in it, we need to remove them with the -r flag. We can do this with the command below:
rm -r vibes/
The command above deleted the “vibes” folder including all the directories associated with it.
You can also delete multiple folders with rm. You can do this by specifying multiple directory names after rm. Let’s say you want to remove the “vibes” and “music” folders from your current working directory. You can do this with the following command:
rm -r vibes music
How you can Force delete a Directory and the contents in it
By default, the rm -r command prompts you to confirm the deletion of a file or directory. This can happen if the file you are trying to delete is already protected. To override this, you can specify the -f flag as follows:
rm -rf vibes
This command will make sure that the directories and subdirectories are permanently deleted in the “vibes” folder. When you run this command, you will not be prompted to confirm that you want to delete files. Therefore, you should use this command occasionally and only when you are very sure that you want all the contents of the folder deleted.
Delete Linux Directories Using The rmdir Command
With the rmdir command, you can delete an empty directory on Linux. This is the recommended command if the folder you want to delete does not have anything in it, meaning that it is empty and you want it removed from your system.
Let’s say our current working directory contains the following files and folders (which we can view with the Linux “ls” command):
The “Config” folder is empty. Let’s say you want to remove the “config” folder from your current working directory. You can do this with the following command:
If we run ls to view the contents of the current directory, our command returns:
You will notice that we do not have the “config” directory any longer. It is important to note that the rmdir command cannot be used to delete directories containing files in Linux. If our “config” folder contains a file, the following error is returned:
rmdir: config/: Directory not empty
The rm and rmdir commands are used in Linux to delete a directory. rm is used to delete non-empty directories. The rmdir command is used to delete empty directories. It cannot be used to delete folders containing files.
For more information about these commands, type “man” followed by the name of the command into the terminal. This will allow you to view the manual page on Linux for the command.
Before deleting files, it is important that you have selected the correct files to delete. When you use commands like rm or rmdir, there is no way to go back.
- Free Online Courses at Harvard University with Certificate
- How to Fix Google Classroom stream Disappeared Problem
- How To Download Udemy Courses Offline on PC and Android Device
- OneNote in Microsoft Teams: How to Add/Use OneNote in Microsoft Teams
- How to Create a Google Classroom: Guide to Create Classes and Contents