Follow ZDNET: Add us as a preferred source on Google.
Linux makes interacting with your PC and your apps quite straightforward. Anything you can do on proprietary operating systems like Windows and MacOS, you can do with the open-source OS, from the simple to the complicated.
Often, however, the simplest tasks can be a bit confusing, especially when you’re new to something. For instance, how do you delete a directory (aka “folder”) on Linux?
Also: The first 8 Linux commands every new user should learn
Sounds simple, right? It is! Just like on MacOS or Windows, deleting a directory on Linux can be handled by anyone, regardless of skill level.
I’ll show you two easy ways to do this, and I’ll also include a bonus method that provides a more secure way to delete a directory.
Method 1: From the file manager
What you’ll need: The only things you’ll need for this are a desktop Linux distribution and a directory to delete. I’ll demonstrate this on Pop!_OS with the COSMIC desktop, but the process will be similar, regardless of Linux distro or desktop environment.
I recommend creating a test directory to avoid accidentally removing the wrong one. To create a new directory within the file manager, right-click on any blank spot in a directory and click New Folder. If you’re using KDE Plasma, you’d right-click a blank spot in the file manager and then click Create New > Folder.
Let’s delete the test directory you just created. Open your file manager and locate that directory.
Right-click the directory to be deleted and then click Move To Trash. If you don’t see an entry for Move To Trash, you should see a Delete entry.
Deleting a file from within the Pop!_OS COSMIC file manager.
Screenshot by Jack Wallen/ZDNET
This process works for both empty directories and directories that house files/folders.
Also: My top 5 password managers for Linux – and my favorite works on Windows and MacOS too
You can then empty your trash so the file is permanently deleted.
Method 2: From the command line
Deleting a directory from the command line is a bit trickier, but not overly difficult. This is done with the rm command.
Open your terminal app
The first step is to open your terminal app.
Change into the correct directory
Next, change into the location that houses the directory to be deleted. For instance, say the directory to be deleted is located in /home/jack/Documents. To change into that directory, issue the command:
cd ~/Documents
The ~/ is shorthand for your home directory.
Note: You don’t have to change into the location housing the directory to be removed. If you don’t, you’ll just need to use the full path to the directory to be deleted.
Delete the directory
Let’s say the directory to be deleted is named TESTING. To delete that directory, regardless of whether it’s empty or contains files/folders, you would delete it with the command:
rm -rf TESTING
If you were to use only the command rm TESTING, it would fail because you’re deleting a directory. The -rf options are:
- r – recursive – deletes all files/folders within the directory and then deletes the directory itself.
- f – forces the deletion.
You don’t have to use the f option. For whatever reason, I just automatically use it.
You could also use the interactive mode, which would be:
rm -ri TESTING
When deleting in interactive mode, you will be prompted to OK every step.
Bonus method
For some, using one of the above methods isn’t enough, especially if a directory contains files with sensitive information that need to be deleted.
Also: 5 obscure Linux distros you’ve probably never heard of – but should definitely try
If that’s the case, you’ll want to use the shred command to overwrite the internal files/folders with 1s and 0s and then delete the directory with one of the methods above.
The shred command works like this:
shred -u -z -n 3 FILE
Where FILE is the name of the file to be deleted.
The above options are as follows:
- -u – Deallocate and remove the file after overwriting.
- -z – Add a final overwrite with zeros to obfuscate the shredding.
- -n X – (where X is a number above 3) indicates how many iterations are used for the overwriting. The default is 3.
After you’ve shredded all of the containing files, delete the directory with your file manager or the rm command.