Understanding how to delete a file in Linux is an important part of a keeping your computer’s file system well-organized. Without regular maintenance, hard drives can quickly become disorganized tangles of unused files, archives, and program artifacts that can slow your computer down.
Since a program can’t know what to safely delete, it’s up to the user to manage this important task. Fortunately, Linux provides several ways to do it. So, whether you’re using a desktop file manager or working from the terminal’s command-line interface, we’ll show you how to delete a file in Linux operating systems quickly and safely.
Anyone coming from Windows or macOS will find Linux’s desktop environments to be very familiar. The file explorer on most Linux desktop environments like Ubuntu’s GNOME or Mint’s Cinnamon does make Linux file management easier for the average user who may not be as familiar with command-line interfaces.
Once you’re in the file explorer and you’ve found the file or files you want to delete, you have two ways you can go. First, you can move it to Trash, which is a special container on the file system that stores files and directories you no longer want.
You can do this by left-clicking on the file and hitting the “Delete” key on your keyboard. This works for groups of files that you’ve selected them with a selection box or CTRL/SHIFT + left-click. Alternatively, you can right-click on the file or one of the files you’ve selected and select “Move to Trash” from the context menu.
Once the files are in the Trash container, you’ll need to empty it manually to permanently delete the files inside of it. You can do this by right-clicking “Trash” on most file explorer’s navigation side bar and selecting “Empty the Trash.”
Double-clicking on it will take you inside the container, where you’ll have the option of restoring files to their original locations or permanently deleting them all.
Some desktop environments, like Mint’s Cinnamon, also give you the option of deleting a file permanently right from the context menu, bypassing the Trash container entirely.
This should only be done if you are certain you want to get rid of a file because while you might be able to recover a “permanently” deleted file from a hard drive, it isn’t a guarantee and the process can be very complicated and time consuming – and possibly expensive.
It’s always safer to move something to Trash than deleting it permanently right off the bat. By leaving it in Trash, you retain the option of changing your mind if you find that you needed the file.
In most cases, by the time you get around to emptying the Trash container, you’ll already have realized that you needed something and restored the file. If you haven’t by the time you go to empty the Trash, it’s much more likely that you really didn’t need it.
The Linux command-line interface has an unearned reputation for being overly technical. Its flexibility allows for commands and scripting as complicated as one needs it to be, but the secret is that Linux commands are themselves rather simple to use.
They offer much more functionality than their GUI counterparts, so knowing how to delete a file on a Linux command-line makes file system management significantly easier.
To delete a file, you need to use is the rm command and tell it what file or files you want it to delete. If you are in the directory containing the file to be deleted, you only need to type:
While that is useful, you can also delete several files at once by using wildcard characters, so to delete all the files in the current directory with the .txt extension, type:
By passing the command different option flags, you can also change its behavior, so if you wanted to go through every .txt file in the current directory and receive a prompt to confirm the file deletion, type:
rm -i *.txt
There are several command options available, including –help which will list all the ways you can use the command.
Keep in mind, there is no Trash container with the rm command, so once a file is deleted, there’s no getting it back. This makes it a powerful tool so you need to be careful about how you use it, especially when deleting files using wildcards – you might delete way more than you intended. To learn more about How to Find Files and Folders on Linux, you can go through our in-depth article.If you are still facing any issue then do let us know in the comment section below. We will definitely try to help you out.