How to Delete a File in Linux

September 7, 2023

Introduction

Deleting unnecessary files in Linux is a routine task and is essential for maintaining a clean and functional system.

Find out how to remove files in Linux using straightforward commands like rm and unlink.

To complete this task, you need access to the command line and a user account with sudo privileges.

Deleting files in Linux.

Note: If you feel that a directory is misplaced and you do not want to remove it, try moving it to a different place. To learn how, visit our post How to Move Directories in Linux.

How to Delete Linux Files

The rm and unlink commands are standard utilities that come with the Linux operating system and should work across all Linux distributions. The commands and examples in this guide are demonstrated using Ubuntu 22.04.

Delete a Single File

There are two main options for deleting a single file in Linux:

Option 1: Delete a File by Using the unlink Command

The unlink command is a simple utility designed to delete a single file without using additional options. It is typically used in scripts and automated tasks.

Note: When a file is deleted via unlink, it is not easily recoverable unless you have a backup. Proceed with caution.

The unlink command in Linux has the following syntax:

unlink [file_name]

For example, enter the following command to delete a file named examplefile1 from the current directory:

unlink examplefile1

By default, the terminal does not provide feedback when using unlink to delete a file.

Option 2: Delete a File by Using the rm Command

rm is a versatile command allowing users to delete single or multiple files and directories in Linux. It supports advanced features such as wildcards, interactive prompts, and recursive deletion.

Note: Once the rm command has deleted a file, you will not be able to access it. The only way to retrieve a file would be to restore it from a backup (if one is available).

The rm command in Linux has the following syntax:

rm [option] [file_name]

Enter the following command to delete examplefile2 from the current directory:

rm examplefile2

To delete a file in a different directory, either switch to that directory or specify the file location in a single command. For example, use the following command to delete examplefile3 from the Documents directory within the Home folder:

rm ~/Documents/examplefile3

The rm command does not provide responses by default. If you need confirmation that a file was deleted, display the progress of the deletion with the -v option:

rm -v examplefile4
A verbose response from the system when using the rm command to delete files.

The system informs you that examplefile4 has been removed.

When deleting a read-only file, the rm utility might prompt for confirmation.

Deleting a read-only file using rm in Linux.

You can bypass this prompt by adding the -f option:

rm -f examplefile5

Users who attempt to delete a file they do not own might encounter an Access denied message. To delete such a file with elevated permissions, use the sudo command:

sudo rm examplefile6

The sudo command grants elevated privileges, enabling regular users to perform actions like file deletion, which they would not typically have permission to execute.

Delete Multiple Files

The rm command can also delete more than one file at a time. To delete multiple files, enter the rm command and list the files with spaces between:

rm examplefile7 examplefile8 examplefile9

rm supports the use of wildcards. For example, to delete all files with the .bmp extension, enter:

rm *.bmp

This method can also be used to delete all files that contain a specific string of characters:

rm *sample*.*

The system will search the current directory for all files that contain the word sample and erase them.

Note: Be cautious when using wildcards like (*), as they can unintentionally match and delete important or hidden files.

If you are deleting multiple files and want to trigger a confirmation prompt, use the -i option:

rm –i examplefile*
A rm prompt asking for confirmation before a file is deleted in Linux.

The system asks for confirmation before deleting each file. Users can type y (yes) or n (no) in response.

Note: Read about sudo rm -rf and why it is a dangerous Linux command.

Conclusion

You have successfully deleted a single and multiple files in Linux using the rm or unlink command.

Check out our detailed guide for deleting directories in Linux if you need assistance with removing entire folders.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Find Files in Linux With the Find Command
December 19, 2018

The find command is a useful command-line tool in Linux. It allows you to use the terminal to search for a...
Read more
How to Kill a Process in Linux? Commands to Terminate
December 13, 2023

If a Linux process becomes unresponsive or is consuming too many resources, you may need to kill it. Most...
Read more
Chown Command: Change Owner of File in Linux
April 29, 2019

The chown command changes user ownership of a file, directory, or link in Linux. Every file is associated...
Read more
How to Copy Files and Directories in Linux
December 28, 2023

Want to learn how to copy files in Linux OS? This guide will show you how to use the Linux commands to copy...
Read more