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 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

1. To delete a file temporarily, open a file manager of your choice and navigate to the location of the files you wish to delete.

2. Then, select the files you want to delete and press the “Delete” key on the keyboard.

files selected for deletion

3. Alternatively, you can right-click on one of the selected files and select the “Move To Trash” option.

Move to trash menu

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 <options> <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.

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 single file using rm command

Note:

If you’re in the same directory, you can simply write the file name instead of writing the 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 multiple files using rm command

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>

prompt before deleting the files

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>

Command to force delete write-protected files

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:

  1. ? character: This will match with any single character only. For example, if we provide input as te?t.txt, then the ? character will match with any character in the file names starting with ‘te’, ending with ‘t’, and having one character in between.
  2. * character: This will match any character any number of times in the given string. For example, if we give input as t**t.txt, the ** character will match with any character any number of times in the file names starting with ‘te’ and ending with ‘t’.
  3. [] character: This match only the characters specified within the brackets. For example, if we give input as te[ab]t.txt, then this will match only as teat.txt and tabt.txt with the file names present in the given directory.

We can use wild cards in a variety of commands, including the rm command, as shown below:

rm <wildcard>.<extension>

It is always advisable to run the ls command 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 the rm command with the wildcards.

checking and deleting files using wildcards

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>

unlink command usage

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 file using the shred command

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 "<filename>" -exec rm {} \;

In the above syntax, the find command looks for the filename and then passes the search results to the rm command, which deletes the files. The backslash is used to treat the semicolon as a command termination.

delete a file using the find command

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.

How do I delete content from a file in Linux?

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>

How do I delete empty files in Unix?

To delete empty files in a directory, use the following command:
find . -type f -empty -print -delete

Why can’t I delete a file in Linux?

While deleting a file, if you see an error like “permission denied”, it means you do not have “write permission” for modifying the file.

#Tags
comment Comments 0
Leave a Reply

Loading comments...