Our second quick and short tutorial. Before, we showed you how to rename a file in Linux. Now we’re going to show you how to remove a directory in Linux.
Like most other basic commands, these will work on most Linux-based distros, including CentOS and Ubuntu. The most common usage is on cloud servers.
Generally, you’ll use the “rm” command to remove files and directories in Linux. But, there are different use cases and different commands, so we’ll go through them below.
Remove an empty directory
To remove an empty directory (folder), you can use the “rmdir” command. The syntax is:
rmdir directoryname
So if you had an empty directory named “test”, you can remove it with:
rmdir test
But, if the directory had files in it, you would not be able to remove it with “rmdir”. If you tried removing a directory with files, you would get an error like this:
rmdir: failed to remove 'directoryname': Directory not empty
Instead, you should use a different command.
Remove a directory with files and subdirectories (non-empty directory)
Here’s where we would use the “rm” command. You can also remove empty directories with the “rm” command, so you can always use that one.
So, if we had a directory with subdirectories and files in it, we would use this syntax:
rm -r directoryname
We used the option “-r” to recursively delete all subdirectories (subfolders) and files in the parent directory.
If you don’t use the “-r” option, you will only delete a single file. With our command, we deleted all contents (files and directories) within the parent directory.
Remove a single file
To remove a single file, you can use the “rm” command with no additional options:
rm file.txt
And that’s it. Those were the most common commands for removing a directory in Linux.
Tip: don’t always use the -f option
When using the “rm” command, you should not always use the “-f” (force) option. By using that option, you won’t get any warnings or prompts when removing something. And it’s always better to have a warning or a prompt before removing something, just to make sure you’re doing the right thing.
If you get a “permission denied” (or similar) error
If you get a “permission denied” error or any other similar error, then you are probably using a non-root user. You should either use the root user or use sudo to run the commands. So just append “sudo” to each command and enter your sudo password when prompt:
sudo rm -r directoryname
9 thoughts on “How to Remove a Directory in Linux”
For those who would rather use the Trash, instead of deleting permanently, consider using `gvfs-trash`. To empty the Trash, use `gvfs-trash –empty`.
rmdir works where the directory is empty and stops when it finds a directory with files, it doesn’t skip or work recursively so the command is not much use going one by one.
find . -type d -empty -delete (is better)
Thanks!
that’s Very useful thanks
you can open the super directory as root and delete inside directories as usual
thanks for the explanation
Thanks for the explanation, that’s very usful.
THANK YOU! I tried few methond and only this one worked 😉
sometimes it’s faster to just (very carefully!!)
$ sudo thunar
then use thunar in root mode to manually do whatever you need to do.
when you’re done, quit that instance of thunar and then
$ exit
to close up the console window.
if you don’t have thunar, it still works with pcmanfm or whatever you use as a file manager.
be sure to avoid running any programs or deleting or changing any system files. but then again, if you actually need to, this is a good way.
also, just remember that if you’re in root mode, then you’re acting as the root user, so those permissions and ownerships have effect.
if for some reason you accidentally mess some things up, you might be able to fix that with “chmod” and “chown”; but that’s not always simple, so just be very careful and it will be OK.
it’s supposed to be fun and easy to use linux, not tempermental and fussy and difficult. interactive dialogs are 100% unneeded when just doing basic file stuff.