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
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
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
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.
unlink command in Linux has the following syntax:
For example, enter the following command to delete a file named examplefile1 from the current directory:
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).
rm command in Linux has the following syntax:
rm [option] [file_name]
Enter the following command to delete examplefile2 from the current directory:
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 command does not provide responses by default. If you need confirmation that a file was deleted, display the progress of the deletion with the
rm -v examplefile4
The system informs you that examplefile4 has been removed.
When deleting a read-only file, the
rm utility might prompt for confirmation.
You can bypass this prompt by adding the
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 rm examplefile6
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
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:
This method can also be used to delete all files that contain a specific string of characters:
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
rm –i examplefile*
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.
You have successfully deleted a single and multiple files in Linux using the
Check out our detailed guide for deleting directories in Linux if you need assistance with removing entire folders.