How to delete a directory in Linux
How to delete a directory in Linux using file explorers or terminals
Understanding how to delete a directory in Linux systems is an important part of a keeping your computer's file system efficient and free of clutter. Fortunately, Linux makes the process simple, even when working from the terminal's command-line.
While most directories on your computer's file system are created and modified by the programs you run, a program will almost never delete one on its own. That is user's responsibility and it's one users should take somewhat seriously.
- Discover the best Linux distros for beginners
- These are the best Linux apps of 2020
- Find the best Linux laptops on the market
- What are the best Linux distros for privacy and security in 2020?
Without managing your computer's file system, it can quickly become a tangled, disorganized mess of unused files, unnecessary archives, and empty directories extending down to nowhere.
Knowing how to delete a directory in Linux is especially important for keeping your file system organized so you and your programs can find files when you need them.
How to delete a directory in Linux systems like Ubuntu and Mint
Linux comes in many different forms, usually called distributions or flavors. The most popular distributions of Linux, Ubuntu and Mint, will look very familiar to anyone who is used to Windows 10 or macOS.
These versions of Linux use desktop environments with file system explorers that let you view, add, modify, and delete the various files and directories stored on your computer's storage drive in a very intuitive way.
The easiest way to delete a directory in Linux Ubuntu or Mint is to simply right-click the folder in your system's file explorer utility and select the option "Move to Trash."
Get the best Black Friday deals direct to your inbox, plus news, reviews, and more.
Sign up to be the first to know about unmissable Black Friday deals on top tech, plus get all your favorite TechRadar content.
This moves the directory and everything in it into a special Trash container in the file system that stores the unwanted data.
This allows you to remove unnecessary directories to make things more organized while also giving you the opportunity to restore the directory to its original location if you change your mind afterward.
Some flavors of Linux with desktop environments might also include a "delete" option in its right-click context menu as well, which deletes a file or directory immediately, bypassing the Trash container entirely.
Recovering this data is sometimes possible if you change your mind, but that is not guaranteed. This option, if available, should only be used if you are completely certain that you won't need that directory and its contents later.
Since files and directories moved to the Trash container still take up storage space, you will eventually want to empty the Trash container to erase the data from the computer's storage drive.
This can be done by right-clicking the Trash container itself, found either on the system desktop or in the sidebar of the file system explorer. In either case, a context menu will appear that will give you the option to empty the Trash container. You can also click into the Trash container and restore contents to their original location or delete everything in it permanently.
How to delete a directory using a Linux command line terminal
The desktop file system interface is an intuitive way to managing files and directories, but it isn't the only one - or even the best one, for that matter. All Linux systems have a built-in command-line interface that you can use to delete a directory with a single command.
In many ways, Linux terminal commands are more versatile and powerful than their desktop counterparts. That makes knowing how to delete a directory on Linux systems using the command-line a very useful skill, one that you'll find yourself using regularly to perform major file system maintenance tasks.
There are two different commands that you can use to delete a directory from the command line: rm and rmdir.
The rmdir command is specifically for deleting empty directories, so it won't delete files or directories that contain files, but it will delete directories that contain subdirectories so long as these are also empty. The basic syntax of the rmdir command looks like this:
rmdir options directory
You can enter rmdir --help into the terminal to learn more about the different options you can pass to the command.
- Learn how to better manage the Linux file system using Linux commands
The other command you can use to delete a directory is rm. The basic syntax for the rm command is:
rm options name
By default, rm only deletes files, so you need to tell it to delete a directory using -d, like this:
rm -d directory
The rm command is more robust than rmdir as well, so you can use it to recursively delete a directory, its subdirectories, and all the files it contains with a single command, like this:
rm -dr directory
You can also pass rm the -i option if you want to be prompted for confirmation before it deletes a file or directory, letting you delete some files and directories in a tree but not others.
When using either of these commands, it does help to be in the parent directory of the one you want to delete but isn't necessary. To delete a directory that isn't inside the current working directory, you can prefix the directory's name with a relative or absolute pathname.
- Conquer your fear of the command line and learn how to use Linux commands
John (He/Him) is the Components Editor here at TechRadar and he is also a programmer, gamer, activist, and Brooklyn College alum currently living in Brooklyn, NY.
Named by the CTA as a CES 2020 Media Trailblazer for his science and technology reporting, John specializes in all areas of computer science, including industry news, hardware reviews, PC gaming, as well as general science writing and the social impact of the tech industry.
You can find him online on Threads @johnloeffler.
Currently playing: Baldur's Gate 3 (just like everyone else).