How To Delete A File In Linux 5 Methods
Below, we have detailed the methods to delete a file via the file manager and some Linux commands to accomplish the task. We are using Ubuntu 20.04 LTS and Nautilus file manager for this tutorial but rest assured as these methods will work on any Linux distribution.
Delete a File Using File Manager in Linux
Delete Files Temporarily in Linux
- To delete a file temporarily, open a file manager of your choice and navigate to the location of the files you wish to delete.
- Then, select the files you want to delete and press the “Delete” key on the keyboard.
- Alternatively, you can right-click on one of the selected files and select the “Move To Trash” option. All files deleted using the file manager are moved to a new location known as “Trash,” which is similar to Recycle Bin in Windows.
Delete Files in Linux Permanently
To permanently delete files in Linux using a file manager, select the files you want to delete and press the “Shift + Delete” keys together. It is also a good practice to empty the “Trash” from time to time to restore much-needed storage space on your Linux device.
Delete a File Using the Terminal in Linux
The command line method to delete files is the fastest method of the two. Here, we have discussed four easy-to-use commands, including rm, unlink, shred, and find, to delete files in Linux.
How to Use the rm command in Linux
First, let’s look at the rm command. It is a versatile command that can be used to delete files as well as directories and offers a ton of options to work with. The basic syntax of the rm command is:
rm
Delete Single File
To delete a single file irrespective of the file location in Linux, use the following command:
rm <path_to_the_file>
Delete Multiple Files
To delete multiple files existing in different directories, you simply need to paste the file locations after the command separated by empty spaces. Here’s how you can use the following command:
rm <path_to_the_file_1> <path_to_the_file_2> <path_to_the_file_3>
Delete Files with a prompt
Generally, the rm command only gives a prompt when deleting a write-protected file. To get a prompt before deleting every file, use the -i flag with the rm command as shown below:
rm -i <path_to_the_file>
Force Delete Files
If you do not want to see any prompt when deleting some files, use the -f to forcefully delete the files as shown below:
rm -f <path_to_the_file>
Even after using the -f flag, if you see an error stating “Permission Denied” use root privilege with the sudo command as shown below:
sudo rm -f <path_to_the_file>
Delete Files using Wildcards
In Linux, we can use wildcards to match and delete a file. Wildcards are special characters that recognize a specific naming pattern and work for both files and directories. There are three types of wildcards:
We can use wild cards in a variety of commands, including the rm command, as shown below:
rm
Delete Files Using the unlink Command
The unlink command in Linux does not have many options and can only delete a single file at a time. The basic syntax of the unlink command is as shown below:
unlink <file_name>
Delete Files Using the shred Command
Normally when we delete a file in Linux using any command, only the pointer which points to the memory block gets deallocated but the file contents still exist in the memory. This enables many recovery tools in recovering deleted files. If you want to permanently delete files from the memory and leave no trace, you should use the shred command. It obfuscates the file contents multiple times and then deletes the file, making it nearly impossible for any recovery tool (even with advanced hardware) to recover the file.
To delete a file permanently in Linux, use the following command:
shred -uz <file_name>
Here, -u is used to delete the file and -z is for overwriting a file with zeroes to hide the shredding, thus, leaving no trace of the file.
Delete Files Using the find Command
The find command can be used to delete files when you do not know their exact location. The syntax to delete files using the find command in Linux is:
find . -name “
In this article, we have shown some easy steps to delete files in Linux using both the GUI as well as the Terminal. We hope this article was helpful in teaching how to use commands like find to not only search for but also delete files when used with the rm command. Further, do remember to double-check the files before deleting them, or else you could lose access to important personal data. And if you’re cozying up to the Terminal, we suggest you also go through our in-depth guide on how to rename a file in Linux. That said, do let us know your most-used Linux commands in the comments section below.