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.

Remove a File in Linux (2023)

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.

Siehe auch  How to Install RAM on Motherboard in Your PC (2023)

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

The rm command supports the following options:

Option Description
-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.
-d This flag is used to remove empty directories.
-v This 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

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:

Siehe auch  How to Switch to Canary Channel on Your Chromebook

rm

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

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

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

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 .

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:

Siehe auch  How to Open the Control Panel in Windows 11 (9 Methods)

unlink

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

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

Frequently Asked Questions

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.

>

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.

Efficiently Delete Files in Linux

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.

Source link

Anzeige
Benutzerbild Von Dorothea Grace
Dorothea, die einen B.Sc. in Informatik und einen M.Sc. in Medientechnik hat, war in Führungspositionen bei IBM und Logitech tätig. Später wurde sie Senior Partnerin bei HCL und HP. Im Jahr 2020 gründete sie, angetrieben von ihrer Leidenschaft für Technik, Futuriq.de, eine Plattform für zugängliche und umfassende Berichterstattung über Technik. Als Chefredakteurin verbindet sie technische Einblicke mit gesellschaftlichem Bewusstsein, um einen verantwortungsvollen Diskurs über technische Innovationen zu fördern und so einen bedeutenden Eindruck in der Branche zu hinterlassen.

Kommentieren Sie den Artikel

Bitte geben Sie Ihren Kommentar ein!
Bitte geben Sie hier Ihren Namen ein