How to Delete a File in Linux (2024)

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.

Table of Contents

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.

How to Delete a File in Linux (5)

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

How to Delete a File in Linux (6)

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>

How to Delete a File in Linux (7)

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>

How to Delete a File in Linux (8)

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>

How to Delete a File in Linux (9)

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>

How to Delete a File in Linux (10)

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.

How to Delete a File in Linux (11)

Delete Files Using the unlink Command

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>

How to Delete a File in Linux (12)

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.

How to Delete a File in Linux (13)

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.

How to Delete a File in Linux (14)

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.

How to Delete a File in Linux (2024)
Top Articles
Commands
Hulu vs. Netflix Review
Hotels Near 6491 Peachtree Industrial Blvd
Nullreferenceexception 7 Days To Die
Avonlea Havanese
Free Robux Without Downloading Apps
Draconic Treatise On Mining
Best Private Elementary Schools In Virginia
Autozone Locations Near Me
Jessica Renee Johnson Update 2023
Tripadvisor Near Me
10 Great Things You Might Know Troy McClure From | Topless Robot
Thotsbook Com
5808 W 110Th St Overland Park Ks 66211 Directions
Where to Find Scavs in Customs in Escape from Tarkov
Grimes County Busted Newspaper
Ice Dodo Unblocked 76
25 Best Things to Do in Palermo, Sicily (Italy)
8000 Cranberry Springs Drive Suite 2M600
Craigslist Apartments In Philly
Spiritual Meaning Of Snake Tattoo: Healing And Rebirth!
Milwaukee Nickname Crossword Clue
Section 408 Allegiant Stadium
What we lost when Craigslist shut down its personals section
950 Sqft 2 BHK Villa for sale in Devi Redhills Sirinium | Red Hills, Chennai | Property ID - 15334774
24 Hour Drive Thru Car Wash Near Me
+18886727547
Mkvcinemas Movies Free Download
LEGO Star Wars: Rebuild the Galaxy Review - Latest Animated Special Brings Loads of Fun With An Emotional Twist
Craigslist Com Humboldt
Www Violationinfo Com Login New Orleans
The Legacy 3: The Tree of Might – Walkthrough
Despacito Justin Bieber Lyrics
Consume Oakbrook Terrace Menu
Jefferson Parish Dump Wall Blvd
Skyrim:Elder Knowledge - The Unofficial Elder Scrolls Pages (UESP)
USB C 3HDMI Dock UCN3278 (12 in 1)
Dee Dee Blanchard Crime Scene Photos
Busted Newspaper Campbell County KY Arrests
Ezpawn Online Payment
Karen Wilson Facebook
Discover Things To Do In Lubbock
No Boundaries Pants For Men
Chr Pop Pulse
Rocket League Tracker: A useful tool for every player
Publix Store 840
Craigslist Monterrey Ca
Where To Find Mega Ring In Pokemon Radical Red
One Facing Life Maybe Crossword
Craigslist Centre Alabama
Elizabethtown Mesothelioma Legal Question
Yoshidakins
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6270

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.