There are several reasons that you might want to remove a directory in Linux. For example, you may want to improve system performance by freeing up disk space. Or you might simply want a tidier and more organized directory structure, free of unused or unnecessary directories.
Our guide takes you through the process of removing directories in Linux. Firstly, we'll look at folder management in Linux distros, and then we cover some of the basic remove commands you can use on the Linux command line interface.
A word of warning, though: if you remove directories using the command line, then there is no way to undo your action. If you accidentally delete a file or directory, the only way to get them back is to restore a backup you made prior to deletion.
How to remove a directory in Linux
Note: "Directory" and "Folder" mean the same thing for the purposes of this guide, but there are some differences when it comes to how both terms are applied by Linux users. Skip to the "Folders vs directories" section to quickly read more.
How to remove a folder/directory in Linux
We'll be using the Ubuntu distro in this tutorial. However, these steps should also apply to other distros.
1. Delete folders: right-click and delete
If you're using a Linux distribution (distro) like Ubuntu, then removing folders is very straightforward and works the same way as deleting folders in Windows or macOS.
I'm using Ubuntu, but other distros will behave in a similar way. First, open your Files application, then go to the location of the folder you want to remove and right-click.
From the right-click menu, select Delete. In Ubuntu, this is called Move to the Rubbish Bin or Move to Trash Bin (depending on your location). Your folder will then be removed.
2. Recover deleted folders from trash in Linux distro
Removing folders in a distro, rather than using Linux commands, has one very big advantage. Your folder hasn't actually been deleted yet. It has simply been moved to the trash folder.
If you delete a folder by accident, you can easily retrieve it by going to your trash folder, right-clicking, and selecting Restore.
Most operating systems will delete items in trash after a certain amount of time in order to free up disk space. In Ubuntu, the default period is 30 days, but you can change this in settings.
3. Remove empty directories using Linux commands
If you would prefer to remove directories using Linux commands, open your command line interface. In Ubuntu, press Ctrl-Alt-T to open the command line — this varies by distro.
The Linux command used for removing files and directories is rm. So to remove an empty directory (Linux differentiates between empty and non-empty directories) type the command rm -d <directory name> (you need to know the directory name). There's also a command rmdir, which specifically removes empty directories.
In the example above, I removed a directory called "Empty" using the command "rmdir Empty".
4. Remove non-empty directories using Linux commands
Linux distinguishes between empty and non-empty directories in order to minimize the risk of accidentally deleting files and sub-directories.
In the example above, I tried to remove a directory called Assam2 using the rmdir and rm -d commands we covered in the previous steps.
However, because Assam2 contains files, I couldn't use these commands, and Linux returned the message cannot remove Assam2: Directory not empty.
To remove a non-empty file, use the command rm -r.
When I used the command rm -r Assam2, the directory and everything inside it was deleted.
5. Remove directories using wildcards
You can use an asterisk (*) as a wildcard when running a rm or rmdir command.
A wildcard is a character used to represent one or more other characters. Wildcards are often used in commands to perform operations on multiple files or directories that match a specific pattern.
In the example above, I removed two directories, Ceylon and Chai, by running the command rm -r C*.
Be careful when using this because if your wildcard is too general, you risk accidentally deleting directories you didn't intend to.
Folder vs directory: what's the difference?
What's the difference between a folder and a directory?
For the purpose of this guide, there's no actual difference between folders and directories. In Linux, the terms are often used interchangeably, and in practice, they refer to the same concept. Both terms refer to a location on a file system where files are stored.
However, in Linux operating systems, 'directory' is the more traditional term. 'Folder' is associated with graphical user interfaces used by Linux distros like Ubuntu, where folders are accessed through a file manager.
Generally speaking, though, you could use folder and directory interchangeably without causing confusion. In this guide, even though we're talking about the same items, we use 'folder' when using the distro file manager and 'directory' when using the Linux command line interface.
Mastering the art of removing directories in Linux equips you with essential skills for efficient file management. While the process may slightly differ across various Linux distributions, the core principles remain consistent.
Whether you prefer the user-friendly graphical interface of your Files application or the command-line expertise of rm and rmdir commands, you can now confidently and swiftly declutter your Linux system. Enjoy organized file systems and a seamless Linux experience! Just remember to make backups first in case you accidentally delete something you shouldn't have.
Get the BEST of Tom’s Guide daily right in your inbox: Sign up now!
Upgrade your life with the Tom’s Guide newsletter. Subscribe now for a daily dose of the biggest tech news, lifestyle hacks and hottest deals. Elevate your everyday with our curated analysis and be the first to know about cutting-edge gadgets.
Emma Street is a freelance content writer who contributes technology and finance articles to a range of websites, including Tom's Guide, Top10.com, and BestMoney. She has over 15 years of experience working in technical development within the fintech industry. When not writing about tech, Emma can be found writing romantic fiction, reviewing films, and updating her tea blog.