How to Delete a File in Linux

In Linux, deleting a file is a common operation performed to manage and organize the file system. The primary purpose of deleting a file is to remove unnecessary or redundant data, freeing up space on the storage medium. 

When a file is deleted from the Linux system, it removes the inode (metadata of the file) and marks the data blocks previously taken by the file as available for new data. This process does not erase the data immediately; it is captured on the disk until overwritten by new information.

This tutorial will explore the several methods to delete files in Linux, along with their syntax and descriptions.

How to Delete a File in Linux 

To delete a file, you typically need to write permission on the directory that contains the file. However, if the file has special permissions or attributes, you need to change these by following the Understanding File Permissions guide. 

Let’s delete the file “ilf_file.txt” that is located in the home directory:

ls -l
How To Delete A File In Linux 1

In the above-highlighted box, enlist columns-wise information:

-rw-rw-r–: File permissions (user: read/write, group: read/write, others: read)

1: Number of links to the file

ilf: Owner of the file

ilf: Group that owns the file

19: Size of the file in bytes

May 27 07:39: Date and time of the last modification

ilf_file.txt: Name of the file

Important: Deleting a file in Linux without read and write permissions on the file itself isn’t possible for a standard user. If the file requires administrative privileges to delete, use the “sudo rm <filename>” command.

Method 1: Using the rm Command

In Linux, the rm command is an efficient way to delete files. It stands for remove and can be utilized to delete one or several more files from the filesystem. If users have write permissions for the directory, execute the “rm” command to delete the file.

Syntax

rm [options] file

Delete a Single File

To delete a single file, simply type rm followed by the file name. For instance, the “ilf_file.txt” file is specified to delete a file from the home directory:

rm ilf_file.txt
How To Delete A File In Linux 2

Delete a File (If Located in Other Directory)

Users can also specify the path of the file with the “rm” command if it is placed in a different directory. Let’s delete a file that is located in the “ILF” sub-directory under the “Downloads” directory:

rm ~/Downloads/ILF/ilf_file.txt
How To Delete A File In Linux 3

Delete Multiple Files 

You can delete several files for instance by listing them with spaces. Let’s specify three files “ilf_file1”, “ilf_file2” and “ilf_file” to delete them:

rm ilf_file1.txt ilf_file2.txt ilf_file.txt
How To Delete A File In Linux 4

Delete Files with a Certain Extension

Users can also delete files with a particular extension. Let’s delete all files having .txt extension:

rm *.txt
How To Delete A File In Linux 5

Delete a File With Confirmation  

To delete files interactively, prompting for confirmation. Users can utilize the -i (interactive) utility that prompts for authentication before removing the file. Let’s delete an “ilf_file.txt”:

rm -i ilf_file.txt
How To Delete A File In Linux 6

Delete Files (Forcefully)

To delete files without prompting, (if files are write-protected), the -f option is utilized as below:

rm -f ilf_file.txt
How To Delete A File In Linux 7

Delete Groups of Files (With Wildcards)

Wildcards can be used with the rm command to match multiple files/delete groups of files. To delete all files that start with “ilf” and end with .txt:

rm ilf*.txt
How To Delete A File In Linux 8

Delete and View the Deletion Progress 

By default, the rm command does not respond. If users require verification that a file was deleted or not, and view the deletion progress with the -v option:

rm -v ilf_file2.txt
How To Delete A File In Linux 9

Delete Directory and Containing Files

For deleting directories that contain files, use the rm command with the -r (recursive) option by mentioning the directory “ILF”:

rm -r ILF
How To Delete A File In Linux 10

Cautious: Be very cautious when using the rm command, especially with wildcards or the -rf option, as it can lead to data loss or system malfunction.

Method 2: Using the unlink Command

The unlink command allows you to remove a single file; it is a simpler alternative to rm when dealing with individual files.

Syntax

unlink file

Delete a Single File

To delete a single file named “ilf_file.txt” in the home directory, use the “unlink” command as below:

unlink ilf_file.txt
How To Delete A File In Linux 11

Delete a File (In a Particular Directory)

To delete a file in a particular directory, use the “unlink” command by the specified path below:

unlink ~/Downloads/ILF/ilf_file.txt
How To Delete A File In Linux 12

In this way, unlink command deletes files in Linux

Method 3: Using the shred Command

In Linux, the shred command is utilized to delete files (securely) by overwriting them with random data before removal, making it nearly impossible to recover the data.

Overwrite and Delete a File 

To delete a file using shred, you can use the -u option. It stands for remove, which tells shred to remove the file after overwriting:

shred -u ilf_file.txt
How To Delete A File In Linux 13

Overwrite and Delete a File (Specified Number)

Additionally, users can also mention the “number of times” the file should be overwritten by using the -n option. To overwrite a file 3 times before deleting it:

shred -n 3 -u ilf_file.txt
How To Delete A File In Linux 14

Note: It is important to exercise caution when deleting files in Linux, as there is no undo button.

Method 4: Using the wipe Command

To securely delete a file in Linux, the wipe command can be used. This command overwrites the file to hide its contents before deleting it, which can prevent the data from being recovered. 

To use wipe, simply install it using the “sudo apt install wipe” command, then type wipe filename in the terminal, replacing the file name with the name of the file you wish to delete:

wipe ilf_file.txt
How To Delete A File In Linux 15

Note: It is important to note that once a file is wiped, it cannot be recovered, so use this command with caution. 

Method 5: Using the srm Command

To remove a file in Linux using the srm command, you would type “sudo apt install srm” in the terminal or command line interface. The srm command is part of the secure-delete suite of tools, which are designed to permanently remove files from your system by overwriting them. 

To use srm to delete a file named ilf_file.txt with a single overwrite pass, you would type:

srm -s ilf_file.txt
How To Delete A File In Linux 16

This ensures that the files cannot be recovered. 

Method 6: Using the Graphical File Managers (GNOME)

Most Linux distributions come with a file manager that offers users to delete files using a GUI, similar to the recycle bin in Windows.

Navigate to the file, right-click it, and select Delete or Move to Trash;

How To Delete A File In Linux 17

That is all from the guide.

Conclusion

Deleting files in Linux using the command line is a powerful tool. To delete a file in Linux, use the rm command followed by the filename. It’s important to be cautious when using rm, as it permanently deletes files without sending them to a recycle bin. 

Users can also remove files using commands like unlink, shred, wipe, and srm. If you want to make sure a file is gone for good, use shred, wipe, or srm, because these make it hard for anyone to get the file back.