The purpose of this tutorial is to show how to remove a directory and all of its contents on a Linux system. Being able to delete directories (sometimes called folders) is an essential part of managing your file system. Linux allows us to remove any directory that our user has permissions on.
While this is a pretty basic function, there are some important caveats to keep in mind. In this tutorial, you will see how to remove a directory and all of its contents from command line and GUI. You will also see how to deal remove directories that you do not have write permissions on by using root privileges.
In this tutorial you will learn:
- How to remove directory and contents via GUI
- How to remove directory and contents via command line
- How to remove multiple directories at once
- How to remove directory and contents with root permissions
|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 and contents 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 will see is that some of the menus look a little different.
Once you know how to delete directories on one GUI, you will 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 the
Deletebutton on your keyboard.
- The directory and its content have not yet been permanently deleted, but have 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 and contents via command line
rm command (short for “remove”) is used to delete directories (and files, too) on Linux. We must specify the location of a directory, along with the
-r option in our command in order to delete a directory and its content. You can either use the absolute path or relative path to the directory.
rmdelete the directory, as well as its content (files, subdirectories, etc), we can use the
$ 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 will 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
If you do not have write permissions on the directory and contents you wish to delete, you will need to use root privileges or log in to the correct user account that has permissions on the directory. For example, you can use
sudo like so:
$ sudo rm -rf example
In this tutorial, we saw how to delete directories and contents 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.