File management is a fundamental part of any device. In linux the situation is no different eliminate files it is something necessary, either because we need to free up space or because these files are unnecessary.

It may seem like a simple task, but deleting a file in Linux can become a real challenge, especially for those who don’t have much experience with this operating system. And if we talk about the ideal OS for each one, we always recommend seeing which operating system is ideal for you.

Linux offers a variety of methods for deleting a file, including: command line (CLI), graphical user interface (GUI), and several other options. In this article we are going to cover several methods so that you can use the one that suits you best.

Delete files in Linux

In this article we are going to see how to delete files using the Linux command and file manager. In this example we have used Ubuntu 20.04 LTS and the Nautilus file manager, although surely these methods work for any distribution of this operating system.

Using the File Manager on Linux

remove files temporarily

In case we want to delete a file temporarily, we will have to open the file manager and navigate to the location of the file we want to delete.

Select files.

After this we will have to select the files that we are going to delete and press the “Delete” key on our keyboard.

Send to trash.

As an alternative method, we can right-click on one of the deleted files and choose the option that says “Move to Trash”.

In this simple way, all the files that we have selected will be moved to “Trash”, which would be the same as the Windows Recycle Bin.

delete files forever

If we want to delete files permanently in Linux from the file manager, we will have to select the files we want to delete and press the “Shift + Delete” keys at the same time. Another thing that we recommend is to empty the trash for a certain time to improve our storage. Although we remember that, if we empty the trash, the files that were in it will be deleted.

Delete files thanks to the Linux terminal

Next, we are going to see the command line method to delete files. We’ll use fairly simple commands like rm, unlink, shred, and find. We understand that for those who are starting with this OS it may seem somewhat complex, but it is much simpler than you might imagine.

“rm” Command

Let’s see the “rm” command. It is a versatile command which we can use to delete files and directories. The basic syntax of this command is as follows:

rm

Although this command admits several options, which we will explain below:

  • -f: means force removal. We will not receive the confirmation notification, all non-existent files and directories will be ignored.
  • -i: stands for interactive delete. By using this flag, the command will ask for confirmation from the user every time we delete a file.
  • -r: in this case means recursive delete. By using this, the command will delete the contents of the specified directory.
  • -d: used to remove empty directories.
  • -v: show an explanation of what we are doing at the moment

Delete a specific file

Delete specific file or document.

In order to delete a file, regardless of its location, we will use the following command:

rm

If we are in the same directory, we can always type the file name instead of having to type the path to the file.

Delete multiple files

Delete multiple Linux files at once.

When it comes to deleting multiple files in different locations, all we need to do is paste the file locations after the command, separating each one with spaces, as shown below:

rm

Delete files with a notice

Delete files in Linux with advance notice.

Generally, the “rm” command will only display a message when we delete a file that is write-protected. In case we want to see a warning before deleting the files, we will have to use “-i” with the “rm” command like this:

rm -i

Force delete files

Force delete files.

In case we do not want to see any kind of warning when we delete files, we will have to use “-f” as follows:

rm -f

In some cases an error may appear that says “Permission denied”, in these cases we will have to use the root privilege with “sudo” as we will see here:

sudo rm -f

Delete files using unlink

Unlink command on Linux.

The problem with the unlink command is that it is too simple, it does not have too many options and we will only be able to delete one file at a time. The basic unlink syntax is as follows:

unlink

Using the shred command

Using the shred command.

Generally, when we delete a file, regardless of the command, only the pointer pointing to the block of memory is deallocated, even though the contents of the file still exist in the memory in question.

This works for many file recovery tools. Although if we want to permanently delete files from memory and not leave any kind of trace, we are going to have to use this command.

After using this command, no recovery tool will be able to recover the files that we have deleted with it. Therefore, this method is permanent. And to be able to use it would be the following command:

shred -uz

Here, -u is used to delete the file and -z is to overwrite a file with zeros to hide the “shredding”, leaving no trace of it.

Delete files using find command

Find files to delete.

We can also use the find command to delete files when we don’t know exactly where it is located. The syntax would be the following:

find . -name “” -exec rm {} ;

Obviously, where we see that it says “filename” we are going to have to write the name of the file together with its extension.

Write A Comment