How to Delete a File in Linux

Introduction

How to Delete a File in Linux

[ad_1]

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.

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00