How to Delete a File in Linux
The Primary Command: rm
The standard command for deleting files and directories is rm
(short for remove).
1. Deleting a Single File
The most basic syntax is to use rm
followed by the filename.
rm filename.txt
This will attempt to delete filename.txt
without any confirmation. If the file doesn't exist, you'll get a "No such file or directory" error.
2. Deleting Multiple Files
You can delete multiple files at once by listing them separated by spaces.
rm file1.txt file2.jpg file3.log
You can also use wildcards (*
) to delete groups of files. This is extremely powerful and can be dangerous if used carelessly.
- Delete all
.txt
files in the current directory:rm *.txt
- Delete all files starting with
old_
:rm old_*
- Delete all files (this is the nuclear option—use with extreme caution!):
rm *
3. Important and Useful Options for rm
The rm
command is silent by default. These options make it safer and more verbose.
-i (Interactive Mode) - SAFETY FIRST!
Always recommended for beginners. This option prompts you for confirmation before deleting each file.
rm -i important_document.txt
Output:
rm: remove regular file 'important_document.txt'?
You must type y
for yes or n
for no.
-v (Verbose Mode)
Shows you which files are being deleted, providing confirmation of the action.
rm -v old_log.log
Output:
removed 'old_log.log'
-f (Force)
Forces deletion, ignoring nonexistent files and never prompting for confirmation. This overrides the -i
option. Use this only if you are absolutely sure.
# Will try to delete a file and won't complain if it doesn't exist.
rm -f some_file
4. Deleting Directories (Folders)
By default, rm
cannot delete directories. You need to use specific options.
-r or -R (Recursive)
This is the key option for deleting directories. It tells rm
to delete the directory and everything inside it, including all subdirectories and files. Use with extreme caution.
# Delete a directory and its entire contents
rm -r my_folder/
For safety, it's very common to combine -r
(recursive) with -i
(interactive):
rm -ri my_folder/
This will ask for confirmation before deleting every single file and directory within my_folder
.
Using rmdir
for Empty Directories
There is a separate, safer command only for deleting empty directories. If the directory has anything in it, the command will fail.
rmdir empty_folder/
This is safe because it will not delete any files.
5. A Safer Alternative: Moving to Trash (GUI and CLI)
Unlike desktop environments, the standard rm
command does not send files to a trash bin. It permanently removes them immediately. While tools exist to add a "trash" functionality to the command line, they are not installed by default.
- GUI (Graphical Interface): In environments like GNOME, KDE, or XFCE, you can simply right-click on a file or folder and select "Move to Trash" or "Delete". You can then empty the trash later.
- CLI Tools: You can install tools like
trash-cli
to have a safer delete command.# Install trash-cli on Ubuntu/Debian sudo apt install trash-cli # Use it to "delete" a file to trash trash-put filename.txt # List trashed files trash-list # Restore a trashed file trash-restore # Empty the trash trash-empty
Best Practices and WARNING ⚠️
The Linux command line assumes you know what you are doing. There is no "Undo" for the rm
command.
- Double-Check Your Path: Always verify you are in the correct directory (
pwd
) and are typing the correct filename (ls
). - Use Tab Completion: Press the
Tab
key to auto-complete file and directory names. This prevents typos. - Start with
-i
: Get in the habit of usingrm -i
until you are completely comfortable. You can even create an alias in your~/.bashrc
file to makerm
always interactive:alias rm='rm -i'
- Beware of Wildcards (
*
): The commandrm *
deletes everything. The commandrm -r *
deletes every file and every subdirectory. The infamousrm -rf /
command tells the system to forcibly and recursively delete the root directory, which would destroy your entire OS. Modern systems often have safeguards against this, but never run it. - Backup Important Data: Always have a backup strategy for any important data.
Summary Table
Command | Description |
---|---|
rm file |
Delete a file. |
rm -i file |
Delete a file with confirmation. (Recommended) |
rm -v file |
Delete a file and show what happened. |
rm -f file |
Force delete a file (no errors, no prompts). |
rm *.txt |
Delete all .txt files in the current directory. |
rm -r folder/ |
Delete a directory and all its contents. |
rm -ri folder/ |
Delete a directory with confirmation for each file. (Safer) |
rmdir folder/ |
Delete an empty directory. (Safe) |
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.