1. Introduction
When using Ubuntu or other Linux distributions, deleting files and directories is a common task. However, unlike Windows or macOS, Linux does not have a “Recycle Bin” feature, meaning that files deleted via the command line cannot be easily recovered. In this article, we will provide a detailed explanation of how to use the rm
command to safely and efficiently delete files in Ubuntu. We will also cover tips to prevent accidental deletions and methods for recovering deleted files if necessary.
2. Overview of the rm
Command
The rm
command is the standard command for deleting files in Linux. It allows you to remove specified files and directories. Since deleted files are generally not recoverable, careful use of this command is essential.
2.1 Basic Syntax of the rm
Command
rm filename
For example, to delete a file named example.txt
, use the following command:
rm example.txt
Once this command is executed, the file will be permanently deleted. Unlike the graphical user interface (GUI) “Trash” feature, files removed with this command are not temporarily stored elsewhere. Therefore, always double-check before deleting important files.

3. Options for the rm
Command
The rm
command comes with multiple useful options. Utilizing these options can make deletion operations more efficient and safer.
3.1 -r Option (Recursive Directory Deletion)
By default, the rm
command does not delete directories. To remove a directory along with its contents, including files and subdirectories, use the -r
(recursive) option.
rm -r directory_name
For example, to delete a directory named /example_dir
:
rm -r /example_dir
This option ensures that all files and subdirectories inside the specified directory are deleted.
3.2 -i Option (Confirmation Before Deletion)
To prompt for confirmation before deleting a file, use the -i
option. This helps prevent accidental deletions.
rm -i example.txt
When executed, a message like “Delete example.txt?” will appear. You can respond with “y” (yes) or “n” (no) to confirm or cancel the deletion, reducing the risk of unintended file removal.
3.3 -f Option (Force Deletion)
If a file cannot be deleted normally or prompts a confirmation message, the -f
(force) option can be used. This option forces deletion without any confirmation, making it useful for removing read-only files or those without write permissions.
rm -f example.txt
While this option is powerful, it should be used with caution, especially when deleting multiple files at once or when ignoring error messages.
3.4 -d Option (Delete Empty Directories)
To delete an empty directory, use the -d
option. This works only if the directory does not contain any files.
rm -d /emptydir
If the directory is empty, it will be successfully removed.

4. Important Considerations When Deleting Files
4.1 Preventing Accidental Deletion
Deleting files requires caution. To avoid accidentally removing important files, follow these best practices:
- Enable the
-i
Option by Default: You can set up analias
command to always use the-i
option, ensuring that every delete operation prompts for confirmation. - Always Create Backups: Before deleting critical files, make sure to back them up. Storing copies in cloud storage or external devices reduces the risk of data loss.
4.2 Using the alias
Command
To enable confirmation prompts for every deletion, add the following setting to your .bashrc
file:
alias rm='rm -i'
With this configuration, every rm
command will behave as rm -i
, significantly reducing the risk of accidental deletions.
5. Deleting Multiple Files at Once
If you want to delete multiple files at once, you can use the *
(wildcard) character to remove files matching a pattern. For example, to delete all .txt
files in the current directory, use the following command:
rm *.txt
The *.txt
wildcard matches all files with the .txt
extension. This is useful when you need to delete a large number of files efficiently.

6. Viewing Deletion Logs
If you want to verify which files have been deleted, use the -v
(verbose) option. This option displays messages showing the deleted files, providing a way to confirm your actions.
rm -v example.txt
After execution, a message such as “removed ‘example.txt'” will be displayed. This option is particularly useful when deleting multiple files, allowing you to track the process.
7. Recovering Deleted Files
Recovering files deleted with the rm
command is extremely difficult. However, if you accidentally delete a file, you may be able to restore it using recovery tools such as extundelete
or testdisk
, depending on your file system.
7.1 Recovering Files with extundelete
extundelete
is a tool designed to recover deleted files from ext3/ext4 file systems. Here’s a basic usage example:
sudo extundelete /dev/sdX --restore-file /path/to/file
The sooner you act after deletion, the higher the chance of recovery. However, depending on disk activity, recovery may not always be successful, which is why regular backups are essential.
8. Conclusion
Deleting files in Ubuntu using the rm
command is efficient but carries the risk of accidental deletions. To protect important data, always create backups and double-check before executing deletion commands. Utilizing the -i
option and setting up an alias
can significantly reduce mistakes.