How to Delete a File in Linux
As is the case with any operating system, file management is an important part of using Linux. Sometimes, it becomes necessary to delete files in order to free up space or to remove unnecessary files. While it sounds like a simple task, deleting a file in Linux can be challenging for beginners. In Linux, there are various methods to delete a file, including using the command line (CLI), graphical user interface (GUI), and more. That said, we have covered five different methods to delete files and folders in Linux in this guide. So without further ado, let’s dive right in.
Below, we have detailed the methods to delete a file via the file manager and someLinux commandsto 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 anyLinux 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 filesyou 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 thermcommand in Linux
First, let’s look at thermcommand. It is a versatile command that can be used todelete files as well as directoriesand offers a ton of options to work with. The basic syntax of thermcommand is:
rm <filename_or_directory>
The rm command supports the following options:OptionDescription-f-f stands for forced deletion. With this flag, users will not get a confirmation prompt and all nonexistent files and directories will be ignored.-i-i stands for interactive deletion. When this flag is used, the command will ask for confirmation from the user for each file deletion.-r-r refers to recursive deletion. When this flag is used, the command will remove all the contents of the directory specified.-dThis flag is used to remove empty directories.-vThis flag shows an explanation of what is being done currently.
After executing the command, if there is no output, it means the command has been executed successfully. An error message is printed only when the executed command has run into issues.
To delete a single file irrespective of the file location in Linux, use the following command:
rm <path_to_the_file>
Note:
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>
Generally, thermcommand only gives a prompt when deleting a write-protected file. To get a prompt before deleting every file, use the-iflag with thermcommand as shown below:
rm -i <path_to_the_file>
If you do not want to see any prompt when deleting some files, use the-fto 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 thesudocommand as shown below:
sudo rm -f <path_to_the_file>
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 thermcommand, as shown below:
rm .
It is always advisable to run thelscommand with wildcards to see if you are getting the correct file names. Otherwise, wrong commands could delete your important files. Once you can verify the file names are correct, you can execute thermcommand with the wildcards.
Delete Files Using theunlinkCommand
Theunlinkcommand in Linux does not have many options andcan only delete a single file at a time. The basic syntax of theunlinkcommand is as shown below:
unlink <file_name>
Delete Files Using theshredCommand
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 topermanently delete files from the memoryand 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,-uis used to delete the file and-zis for overwriting a file with zeroesto 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 “” -exec rm {} ;
In the above syntax, the find command looks for thefilenameand then passes the search results to thermcommand, which deletes the files. The backslash is used to treat the semicolon as a command termination.
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 onhow to rename a file in Linux. That said, do let us know your most-used Linux commands in the comments section below.
In order to delete the file contents but keep the file intact, use the following command. Here, the>character is used to redirect the specified contents into the mentioned filename.> <large_file_name>
To delete empty files in a directory, use the following command:find . -type f -empty -print -delete
While deleting a file, if you see an error like “permission denied”, it means you do not have “write permission” for modifying the file.
Beebom Staff
Bringing the latest in technology, gaming, and entertainment is our superhero team of staff writers. They have a keen eye for latest stories, happenings, and even memes for tech enthusiasts.
Add new comment
Name
Email ID
Δ
01
02
03
04
05