How to Delete Files and Directories in Linux
With the passage of time, we tend to accumulate files on our computers that are either not needed anymore or they’ve become obsolete. Similarly, in order to keep running effectively and efficiently, operating systems tend to accumulate files that pile up to consume a lot of disk space. In cases where we need to reclaim some of this disk space, knowing how to delete these files can come in handy.
As always, there are multiple ways to remove files and directories. If it’s just a handful of files, you can always go into the GUI file manager and manually delete them one by one, but if there are more files and you want more control over the delete process you can dive into the terminal and get more control over the whole process. Let’s take a walk through these methods and learn how to delete files and directories and even how to shred them.
The methods and commands will be demoed on Ubuntu 22.04, but you can apply these on other Linux distributions as well.
Method 1 – Using the “rm” command
Short for “remove here” the “rm” command is very useful for removing files and directories from your computer. You can use the variety of options available to remove items as per your needs. Before we start deleting items from your computer, let’s take a look at the syntax for the command:
$rm [item name]
Pretty straightforward right? Let’s see the multiple ways you can delete files:
Deleting a single file
Here is our sample file for this example:
If you have a single file and you want to get rid of it, you can easily get rid of it using the following:
Using the “ls” command, you can see that the file has indeed been removed:
If your file is not located in the same directory, you can specify the file path as well:
Deleting multiple files
Deleting multiple files is very easy as well.
You can directly specify the file names, as such:
$rm test1.txt test2.txt test3.txt test4.txt
Or if you feel like manually specifying all of the names is too hectic, then you can delete all text files using this:
In the command above, you can replace “.txt” with “.jpg”, “.pdf” or any other file extension and the command will remove all of the files with the specified extension from your current directory.
Deleting write-protected files
If you try to delete any write-protected files using the “rm” command, it will prompt you to confirm the operation before removing the file. For instance, let’s make our test file write-protected and then try to remove it:
If you want to forcefully remove the write-protected file, you can use the “-f” option to bypass the confirmation prompt and remove the file from your computer.
$rm -f test.txt
Deleting an empty directory
Deleting a directory is a little different from deleting a file. You need to use different options with the rm command to complete the removal of a directory. If you want to remove an empty directory, you can use:
$rm -d [directory name]
Through this, you can easily remove empty directories from your computer.
You can also use the “rmdir” command in place of the “rm” command to remove an empty directory
$rmdir [directory name]
Deleting directories with content in them
All you need to do is change the option used in the previous example and you can delete directories which have contents within them. You need to use the “-r” option recursively go through the contents of a directory and remove everything. For this example, I have a directory named “Rename” containing three sample files.
So, if I wanted to remove the directory containing my sample test files, I would use:
$rm -r Rename
Deleting multiple directories
Let’s combine both of the last examples and use them to remove multiple directories. You can specify multiple directories with the “rm” command and use the apt options to delete all the specified directories.
$rm -r [directory 1] [directory 2]
Executing these commands will remove all the specified directories whether they have any contents in them or not.
Method 2 – Using the File Manager
If you are not comfortable tinkering with commands within your terminal, you can use the Nautilus File Manager to delete files and directories easily.
Open up your file manager and right-click on the file you want to delete. From the options in the menu, you need to select “Move to Trash”.
Similarly, if you want to remove multiple files, you can just drag your mouse to select multiple files and then right-click to and select “Move to Trash”.
There isn’t any difference in the process of deleting files and directories through the file manager.
For both of these methods, if you want to permanently delete any items, you can select it and then press “Shift+Delete” and you will be prompted to confirm if you want to permanently remove them from your computer.
Bonus – Shred command
This one is for everyone out there who wants to maintain their privacy and keep their data safe if anyone recovers their deleted files. This is like permanently deleting an item but it takes things a step further.
You can shred a file as such:
$shred [options] [file name]
The shred command repeatedly replaces the original content of a file with zeros, to such an extent that if someone recovers your files, they will not be able to access your data. Let’s shred our test file.
$shred -uvz test.txt
The options used here are:
- “-u” option deletes the file.
- “-v” option displays the whole process in your terminal.
- “-z” option overwrites all the contents of your file.
If you want to know more about the “shred” command, you can use the following to know all about the options and the usage of the command:
These are some very basic, yet useful tips and tricks for beginners on how to delete files and directories from Linux.
If you get stuck at any point throughout the guide, you can always engage us through the comment section below and we’ll gladly help you out with your issue. Let us know what you think. Cheers.