You probably already know how to delete files in the command line with the rm command.
rm command can also be used to delete folders (called directories in Linux terminology).
rm -r directory_name_or_path
-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
rm -r directory_name
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
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.
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 -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
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.
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
To summarize what you just learned about deleting folders in Ubuntu command line:
|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|
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 :)