How To Delete Files in Linux/Unix

Learning to utilize the Linux command line is necessary for all Linux users and system administrators. It allows you to perform basic maintenance operations like creating new files, going to certain folders, and relocating existing ones. This tutorial explains how to use the RM command to delete files in Linux. This article will also demonstrate how to remove files in Linux using the file manager (GUI method) and with the best third-party software solution.

Way 1. Deleting Files in Linux Using the File Manager (GUI Method)

If you're new to Linux, you might be hesitant to use the terminal to delete files or folders. After all, there's no going back once you've started deleting files with the rm command. Rather than taking this risk, you can delete files or folders using the file manager included with your Linux distribution, the File Manager.

While the design of Linux file managers varies, they should all function similarly. This guide looks at how to do it with Ubuntu's file manager, but the steps are likely similar for other file managers.

Step 1. To begin, launch your Linux distribution's File manager. This app is known as Files in Ubuntu.

launch the linux file manager

Step 2. Navigate to the directory containing the files or subfolders you want to delete in your distro's file manager. Choose which files or folders to remove first. When you've selected them, right-click on them and choose Move to Trash, Move to Bin, or Delete, depending on your distribution and locale. To achieve the same effect, press the Delete key on your keyboard.

select the files to be deleted

Step 3. Most distributions have a trash system that allows you to "store" files before they are deleted, allowing you to restore them later. It is usually found on the desktop, as a folder in your file manager, or as an app in your software menu. To finish the deletion, navigate to the trash folder and select the Empty or Erase option. Depending on your distribution, these options may differ.

finish the deletion

Step 4. To completely remove your files, select them, then right-click and select Delete from Trash.

delete files from linux trash

Way 2. Deleting Files Using rm Command in Linux Terminal

Using your distribution's file manager app to remove files or folders works fine, but there are some limitations. For example, if you are not the file's owner, you must use the terminal to open your file manager with root access and delete it. A similar app cannot be used on a headless (terminal-only) Linux installation.

This is where the rm command comes into play. This Unix command was created in 1971 and is still the quickest way to delete files or directories on Linux. But be warned: the rm command has a lot of power, especially if you run it with sudo or as the root user directly, so use it with caution.

Prerequisite: Open the Terminal

It should be no surprise that you may remove files in Linux using the command line, given the wide variety of commands accessible in the terminal. To do this, carry out the following actions:

Step 1. Browse the files you wish to delete in the file manager after opening them. Alternatively, you can navigate directly to the files in the terminal by using the command:


Step 2. Open the context menu by right-clicking on the window and then selecting Open in Terminal.

selecting open in terminal

Step 3. To remove a single file or a group of files, use the rm command (short for "remove").

How To Delete a Single File With rm Command in Linux

"rm -i" will prompt you before deleting each file. Consider using rm -I, which will only prompt you once and only if you are attempting to delete three or more files.

Step 1. The rm command, which allows you to delete one or more files simultaneously, is a more commonly used command for removing files.

$ rm {file-name}

rm command

Step 2. If the file is write-protected, rm will prompt you for confirmation before deleting it; otherwise, it will delete it without prompting. To force rm to prompt for confirmation before removing a file, use the "-i" flag:

$ rm -i {file-name}

rm-i flag command

Step 3. The rm command deletes files without producing any output. Using the -v flag on the rm command to see what the rm command is currently doing.

$ rm -v {file-name}

rm-v flag command

Step 4. Using the -f flag to remove write-protected files without prompting.

$ rm -f {file-name}

rm-f flag command

How To Delete Multiple Files With rm Command in Linux

Step 1. You can remove multiple files by passing multiple filenames as arguments to rm.

$ rm {file-name-1} {file-name-2} {file-name-3} ... {file-name-N}

remove multiple files

Step 2. rm also supports regular expressions. To delete all files with the name filename-*, enter:

$ rm file-name*.ext

rm regular expressions

Step 3. Different directories can also be defined using regular expressions. We can use it to delete three files with the names file-name-1, file-name-2, and file-name-3.

$ rm file-name-[123]

delete three files

How To Delete Files With a Specific File Name in Linux

In the terminal, you can delete all Linux files with a specific name. Run the command listed below:

rm *Delete*

This command deletes all files with the name between the asterisks.

delete files with specific file name

How To Delete Files of a Specific File Type With rm Command in Linux

If you have a folder with multiple file types and want to delete all files of a specific type, such as.pdf or .odt, type "rm" followed by an asterisk and the file type.

rm *.odt

In this case, you will delete all files ending in.odt. To delete all files of this type, enter an asterisk followed by a file ending.

delete files of specific file type

How To Delete All the Files in a Folder With rm Command in Linux

Use the command that follows into the terminal to delete all of the files and subfolders in a specific folder:

rm -r Files/*

In this case, all files in the "Files" folder will be deleted. The folder itself will not be deleted. Of course, it can also delete a Linux directory, including all its files.

delete all files in a folder

How To Locate and Delete Hidden Files With rm Command in Linux

Step 1. We can employ the locate command with various options for more complex specifications.

$ find {dir-to-search} -type f -name {pattern} -exec rm -f {} \;

For example,

$ find luv -type f -name "*.txt" -exec rm -f {} \;

locate hidden files with rm command

Step 2. We can modify the above command to delete everything that matches the sequence pattern, including directories within {dir-to-search}.

$ find {dir-to-search} -name {pattern} -exec rm -rf {} \;

delete hidden files with rm command

Step 3. Internally, modern find implementations support the delete feature. The -delete flag overrides the rm instruction, whereas the -depth flag instructs the directory's contents to perform the process.

$ find {dir-to-search} -type f -name {file-name-pattern} -depth -delete

How To Locate and Delete Empty Files With rm Command in Linux

Step 1. To delete all empty directories within a given path dir-to-search, execute the command below:

$ find {dir-to-search} -type d -empty -delete

locate and delete empty files

Step 2. Or use the following command:

$ find {dir-to-search} -type f -empty -delete

execute the another command

How To Recover Accidentally Deleted Files in Linux

With all the deletion methods mentioned above using the rm command in Linux, here is an effective and easy way to recover your accidentally deleted files in Linux. You can use the best third-party software in the market today, the Wondershare Recoverit Linux File Recovery. Data can be lost in a variety of ways. But you don’t need to worry! This one-stop recovery solution can recover from anything.

Wondershare Recoverit - Your Safe and Reliable Linux Recovery Software

5,481,435 people have downloaded it.

Recovers lost or deleted documents, photos, videos, music, emails, and other 1000+ file types effectively, safely, and completely.

Compatible with all mainstream Linux distros, including Ubuntu, Linux Mint, Debian, Fedora, Solus, Opensuse, Manjaro, etc.

Assists in 500+ data loss scenarios, such as deletion, disk formatting, OS crash, power outage, virus attack, lost partition, and many more.

Works through a remote connection. You can recover lost data even when your Linux device is crashed.

Wondershare Recoverit makes Linux data recovery extremely simple. You can recover your data in three simple steps. But if you’re uncomfortable reading the recovery instructions, here is a video tutorial you can watch.

Free Download

For Windows XP/Vista/7/8/10/11

Free Download

For macOS X 10.10 - macOS 13

Step 1. Choose the Linux Recovery

Download and install Wondershare Recoverit on your PC. Start the program and select the NAS and Linux option on the left side of the main interface. To proceed, click the Linux Recovery button.

choose the linux recovery option

Step 2. Connect the Linux System

As shown below, a new window will appear on your screen. Enter the required information to establish a remote connection. When finished, click the blue Connect button.

connect the linux system

Wait for the connection to be established; Recoverit will run an automatic scan to look for your lost files on a Linux computer.

Step 3. Preview and Recover Deleted Files

The scanning process takes some time, depending on the amount of data being scanned; the scanning progress is displayed on the bottom bar. The best part about Recoverit is that you can stop scanning whenever you find the file you want to restore.

scan deleted or lost files

When the scanning is finished, Recoverit lets you preview the files to ensure they are the ones you want to recover. Now, select Recover to save the file to a new location. The software will prompt you to select a location on your computer to save the restored files.

preview and recover files
Free Download

For Windows XP/Vista/7/8/10/11

Free Download

For macOS X 10.10 - macOS 13

Note: For more solutions, here is an article to visit to effectively recover deleted files in Linux.

Tips You Should Know Before Deleting Files in Linux

  • It is essential to exercise caution when using the rm command because it has the potential to permanently delete your files. Always double-check the file name before deleting it to ensure you delete the correct files.
  • Before running the rm command, ensure that you are in the correct directory. Otherwise, it can result in the deletion of files.
  • Unfortunately, once a file is deleted with the rm command, it is permanently deleted and cannot be recovered, so use extreme caution.
  • If you are unsure whether a file is safe to delete, use the "ls -lh" command to obtain more information about it. It will display the file's size and permissions.
  • The "rm -i" command can help ensure that you don't accidentally delete any important files when using the rm or rm -r commands. Before deleting each file, you will be prompted for confirmation.
  • If you're still unsure whether it's safe to delete, you can seek advice on an online forum or chat room. You can always use the cp command to back up your files before deleting them.

Summing Up

Deleting directories and files from the command line in Linux necessitates familiarity with the rm command in the terminal. Remember that in Linux, there is no recycle bin or trash folder. When you use the rm command line to remove files and directories, they are permanently deleted. As a result, proceed cautiously or create a backup using Wondershare Recoverit before deleting files and folders.

You May Also Like

Top 10 Linux Data Recovery Software

Here, we have listed the 10 best Linux data recovery software you can use.

How to Access Linux Partitions from Windows?

Familiarize yourself with the most effective methods to access Linux partitions from Windows in no time.

Top 11 Effective Methods to List/Show Partitions in Linux

This guide presents tested and proven ways to show or list partitions in Linux operating systems.

Top 10 Linux Partition Recovery Tools

Restore your deleted or lost data with the 10 best Linux partition recovery software.

How to Recover Deleted Files from Linux Partition?

Try these tested and proven methods to recover deleted files from Linux partitions.

Other popular Articles From Wondershare