- How to Remove a Directory in Linux
- How to Remove a Directory in Linux?
- rm Command
- rmdir Command
- Remove Directory in Linux – How to Delete a Folder from the Command Line
- Syntax of the Linux rm Command
- How to identify a folder to remove
- Examples of Linux rm command
- How to delete a folder that’s not empty
- How to delete an empty folder
- How to use the -f «force» flag
- Wrapping up
- How to Delete Folders in Ubuntu Command Line
- Delete folders using rm command
- Force remove a directory and its contents
- Removing empty directories with rmdir command
- Conclusion
How to Remove a Directory in Linux
Removing a directory in Linux is a pretty simple task if you are using the GUI. However, if you don’t have access to the GUI, you can also remove directories using terminal commands.
In this tutorial, we will show you how to remove a directory in Linux via commands in the terminal window or command line.
- A system running a Linux distribution.
- An account with sudo privileges.
- Access to the terminal window or command line.
Note: For other Linux directory management articles, see How to Move Directories in Linux and How to Rename a Directory in Linux.
How to Remove a Directory in Linux?
There are two Linux commands you can use to remove a directory from the terminal window or command line:
- The rm command removes complete directories, including subdirectories and files.
- The rmdir command removes empty directories.
It is important to note that the rm and rmdir commands permanently remove directories without moving them to the Trash directory. This means that you cannot restore a directory removed using these commands.
Note: Even though rm and rmdir permanently remove files and directories, users with enough skill and time still have a chance of restoring some of the removed files. If you want to learn more about removing files permanently, have a look at our shred command tutorial.
rm Command
The rm command in Linux removes files and directories.
It uses the following syntax:
rm [options] [file or directory name]
Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces.
The different rm command options include:
- — f : Forces the removal of all files or directories.
- -i : Prompts for confirmation before removing.
- -I : Prompts once before removing more than three files or when removing recursively.
- -r : Removes directories and their content recursively.
- -d : Removes empty directories.
- -v : Provides a verbose output.
- —help : Displays the help text.
- —version : Displays the command version.
Trying to use the rm command without any options to remove a directory results in an error message:
If you want to remove an empty directory, add the -d flag to the rm command:
Note: If you want to remove a directory whose name starts with a hyphen (—), use the rm — [directory name] or rm ./[directory name] syntax.
The example below shows that the rm command with the — d flag removes the Example directory:
Use the -r flag to delete a directory that contains subdirectories and files.
The image below shows the tree hierarchy of the Example directory, which contains Dir1 and Dir2 subdirectories, with multiple text files in each:
Using the -r flag removes the entire directory, including subdirectories and files, while the -v flag lists each step of the process as the output:
The -i option displays a prompt asking you to confirm directory removal. Type Y and press Enter to confirm.
Write-protected directories require user input when deleting. Create such a directory with:
To remove the directory, use:
Type Y and press Enter to confirm deletion. To avoid the confirmation, use the -f flag or elevate the command privileges to sudo:
If the write-protected directory contains other files and directories, use the following command:
Note: rm -rf / is a dangerous Linux command that forces a recursive deletion on the root directory, rendering your system unusable.
Avoid using -f and sudo while removing directories, unless you know what you’re doing. To learn more about this command, visit our article What Is sudo rm -rf in Linux and Is It Dangerous.
rmdir Command
The Linux rmdir command removes empty directories only. The command uses the following syntax:
rmdir [options] [directory name]
The rmdir command includes the following options:
- —ignore-fail-on-non-empty : Doesn’t show an error message when trying to remove a non-empty directory.
- -p : Removes the directory along with its parent in the hierarchy.
- -v : Provides a verbose output.
- —help : Displays help text.
- —version : Displays the command version.
Using the rmdir command on a non-empty directory produces an error:
In this case, the Example directory contains the Test subdirectory:
To remove these directories using the rmdir command, add them in reverse order of hierarchy. Using the -v option lists each step of the process as the output:
rmdir -v Example/Test Example
A simpler method of doing this is to use the -p option with the subdirectory’s name. This removes both the subdirectory and its hierarchical parent:
The rmdir command allows you to remove multiple directories with similar names using wildcards. For instance, if you want to remove directories named Example1, Example2, and Example3:
After reading this tutorial, you should be able to remove directories in Linux using commands in the terminal window or command line.
To learn more about other commands in Linux, check out our Linux commands cheat sheet.
Remove Directory in Linux – How to Delete a Folder from the Command Line
Zaira Hira
Linux CLI is a powerful tool that can help you accomplish complex tasks.
One of the common operations you’ll need to perform is to delete things. Just as like creating files and folders, deleting them from the Linux command line is something you’ll do often.
In this post, we will discuss how to delete directories from the command line. We will discuss the syntax along with some examples. I am using Ubuntu in these examples.
Syntax of the Linux rm Command
You use the rm command to delete something from the command line in Linux. The syntax of the rm command looks like this:
Some important flags you’ll need to use when deleting a directory are as follows:
- -r , -R , —recursive [«Recursion»] – Removes directories and their contents recursively.
- -v , —verbose [«Verbose»] – This option outputs the details of what is being done on the CLI.
- -f , —force [«Force»] – This option ignores nonexistent files and never prompts you.
- -i [«Interactive»] – Use this flag when you want to be prompted before every removal.
- -d [«Directory] – This works only when the directory is empty.
⚠ Be careful when using the rm command️ and always make sure that any important data is backed up.
How to identify a folder to remove
As we are discussing how to delete folders, we need to be pretty sure that we are actually deleting a folder. We can identify a folder/directory with the d flag in the first column. Notice that files have the first flag as — .
Examples of Linux rm command
In our current folder, we have 2 folders CSharpLab and PythonLab . Their content is shown below.
Note that CSharpLab is an empty directory.
How to delete a folder that’s not empty
Let’s delete the PythonLab folder first.
- -r recursively deletes all files and folders. Note in the output below, all files ( man.py, calculator.py, palindrome.py ) and folders ( /lib ) were removed.
- -v shares details.
- -i makes deletion interactive, which means it will ask before removing anything.
How to delete an empty folder
Let’s try to delete the CSharpLab folder. As this folder is empty, we can use the -d flag.
How to use the -f «force» flag
Let’s now see how the -f flag works. This forces the deletion of folders without any prompts or warnings. In case of an error, -v still ignores, and deletes the files that are valid.
In the example below, there is a typo in the folder name. Note that the typo is ignored. The original file is intact.
Wrapping up
Removing directories is useful when you need to remove folders after archiving them, when deleting duplicates, when deleting unused folders, and much more.
All these tasks are targeted at creating more disk space. I hope you found this blog helpful.
How to Delete Folders in Ubuntu Command Line
You probably already know how to delete files in the command line with the rm command.
The same rm command can also be used to delete folders (called directories in Linux terminology).
rm -r directory_name_or_path
The -r option is important and I’ll explain it in the later section of this article.
Creating a new folder in Ubuntu is simple. Deleting them might not be that straightforward, especially if the folders are not empty.
There is a dedicated rmdir command for removing directories. However, it can only delete empty directories. This is why you have to use the rm command for deleting folders.
Let’s see more about removing directories in the command line in Ubuntu Linux.
Delete folders using rm command
On a fundamental level, directories are just special files that have information about the location of files stored in the memory.
This means you can almost use the rm command to remove them, just like regular files. Almost because there is a slight twist here.
If you try to delete directories the same way as files, you may encounter the «rm: cannot remove XYZ is a directory»:
$ rm no_strings_on_me rm: cannot remove 'no_strings_on_me': Is a directory
This is just a failsafe so that you don’t accidentally delete a folder with multiple files inside it.
To purposely delete a folder with the rm command, you have to use the recursive option -r :
This way, you tell the Linux system that you know that you are deleting a directory recursively with all its content. The contents are deleted first and the directory is removed.
Force remove a directory and its contents
A directory may have numerous files and if some of those files are write-protected, you’ll be prompted to confirm the deletion.
$ rm -r new_dir rm: remove write-protected regular empty file 'new_dir/b.txt'?
It’s okay for a couple of files but if it keeps on asking this question for too many files, it will be tiring.
This is when the -f option comes to the rescue so that you can force delete the files. You won’t get promoted anymore.
rm -rf directory_name_or_path
If you use rm -rf dir_name/* it will delete the contents of the dir_name folder but the dir_name will still exist. Why? Because you are explicitely telling the system to delete the contents of directory, not the directory.
Removing empty directories with rmdir command
If you accidentally delete the wrong file, only one file is deleted. If you accidentally delete one directory, multiple files, and even nested directories will be deleted. Which would be a terrible loss and an irreversible one.
The rmdir command exists for this sole purpose. When you use this command, the directory will be removed only if it is found to be empty. If the directory contains any files or sub-directories, the command will stop with an error that the directory is not empty.
Let us first take a look at its syntax:
I have a non-empty directory and an empty directory, as shown below:
$ ls * my_empty_dir: non_empty_dir: string1 string2 string3 string4
Let us see how it looks when I try to remove both of these directories using the rmdir command.
$ rmdir -v my_empty_dir rmdir: removing directory, 'my_empty_dir' $ rmdir -v non_empty_dir rmdir: removing directory, 'non_empty_dir' rmdir: failed to remove 'non_empty_dir': Directory not empty
The rmdir command prevented me from removing ‘non_empty_dir’ because it is not empty.
If you want to stick to using the rm command, it has a «safe» delete option as well. The rm command, when paired with -d option will exit when the directory is not empty.
Since rmdir did not delete ‘non_empty_dir’, let’s see if rm -d deletes it.
$ rm -d non_empty_dir rm: cannot remove 'non_empty_dir': Directory not empty
Conclusion
To summarize what you just learned about deleting folders in Ubuntu command line:
Command | Description |
---|---|
rm -r dir | Delete the dir folder |
rm -rf dir | Force delete the dir folder |
rm -rf dir/* | Force delete the contents but not dir itself |
rmdir dir | Delete dir if it is empty |
The rm command is what I personally prefer, as it is used for the removal of files as well as directories, but rmdir command can be useful too, depending on the scenario.
Do let me know if you have any queries or confusion 🙂