Deleting a directory (also called folder) on Linux is a common task that every user will have to perform at some time or another. This can be done via any desktop environment that you have installed, or from command line with the
While this is a pretty basic function, there are some important caveats to keep in mind. In this guide, we’ll go over several examples of deleting a directory on Linux. Feel free to follow along on your own system in order to master the
rm command and GUI process.
In this tutorial you will learn:
- How to delete a directory via GUI
- How to delete a directory via command line
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to delete a directory via GUI
The process for deleting a directory on Linux, through the graphical interface, is going to vary a little depending on your distribution and desktop environment that you have installed. But the only real difference you’ll see is that some of the menus look a little different.
Once you know how to delete directories on one GUI, you’ll have mastered them all. In the steps below, we’re using the GNOME desktop environment, which is the default for many popular distributions like Ubuntu.
- Right click on the directory you wish to remove, and click “move to trash.” On some desktop environments, the option may simply be called “delete” or something similar. Alternatively, you can highlight the folder and click
Deleteon your keyboard.
- The directory has not yet been permanently deleted, but has rather been moved to the trash bin. If we change our mind about deleting the folder, we can recover it from the bin. To permanently delete the directory, along with any other contents you may have moved to the trash bin, right click on the trash bin icon and press “empty trash.”
That’s all there is to it. Note that this will delete the folder, and all of its contents, including subdirectories. Next, we’ll cover the command line method.
How to delete a directory via command line
rm command (short for “remove”) is used to delete directories (and files, too) on Linux. The most basic form of the command is to simply specify the location of a directory, along with the
-d option in your command. You can either use the absolute path or relative path to the directory.
$ rm -d /path/to/directory
The above example will only delete the directory if it’s completely empty. That’s what the
-d option allows us to do. Without the option, we’ll just get an error that
rm can’t remove directories.
$ rm example rm: cannot remove 'example': Is a directory
If the directory isn’t empty, then the
-d option isn’t going to work either.
$ rm -d example rm: cannot remove 'example': Directory not empty
rm delete the directory, as well as its content (files, subdirectories, etc), we can use the
-r (recursive) option.
$ rm -r example
You may notice how we don’t get much room for error, like we do with the GUI method. There is no trash bin for the command line. To make things a little less risky, we could also use the
-i (interactive) option, which will ask us for verification before deleting. You’ll have to enter
yes in order to proceed with the deletion.
$ rm -ri example rm: descend into directory 'example'? yes rm: remove regular empty file 'example/test.txt'? yes rm: remove directory 'example'? yes
-v (verbose) option if you’d like details about what the
rm command is doing.
$ rm -rv example removed 'example/test.txt' removed directory 'example'
If you have a pesky directory that isn’t deleting easily, or continually prompting you for confirmation, you can use the
-f (force) option to forcefully delete it. Be careful with this one, as it suppresses warnings and will basically delete anything you tell it to, even if doing so is harmful to the system.
$ rm -rf example
You can also remove multiple directories at once. In this example, we delete three different directories in a single command.
$ rm -r dir1 dir2 dir3
In this guide, we saw how to delete directories on a Linux system through GUI and command line. This is a common task that all users should master. As usual, the command line method offers us a bit more control over the process, but both methods are equally viable. Use whichever one is more convenient for you.