How to remove directory and contents in Linux

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
How to remove directory and contents in Linux
How to remove directory and contents in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to delete a directory and contents via GUI



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


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.

  1. 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 Delete button on your keyboard.
    Send the directory and its contents to the trash bin
    Send the directory and its contents to the trash bin
  2. 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.”
    Empty the trash bin to delete all of its contents
    Empty the trash bin to delete all of its contents

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

The 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.




To make 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 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

Use the -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


Closing Thoughts

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.



Comments and Discussions
Linux Forum