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
.txtfiles 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.
Best Practices and WARNING ⚠️
The Linux command line assumes you know what you are doing. There is no "Undo" for the rm command.
Summary Table
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
