How to Delete a File in Linux: Simple Command Guide
To delete a file in Linux, use the
rm command followed by the file name, like rm filename. This command removes the file permanently from your system.Syntax
The basic syntax to delete a file in Linux is:
rm filename: Deletes the specified file.rm -i filename: Asks for confirmation before deleting.rm -f filename: Forces deletion without prompts.
bash
rm filename
Example
This example shows how to delete a file named example.txt and how the system responds when using the interactive option.
bash
touch example.txt ls example.txt rm -i example.txt ls example.txt
Output
example.txt
rm: remove regular file 'example.txt'? y
ls: cannot access 'example.txt': No such file or directory
Common Pitfalls
Common mistakes include accidentally deleting the wrong file or forgetting that rm permanently deletes files without moving them to a trash bin. Using rm -i helps avoid accidental deletions.
bash
rm example.txt # Deletes without confirmation rm -i example.txt # Asks before deleting
Quick Reference
| Command | Description |
|---|---|
| rm filename | Delete a file |
| rm -i filename | Delete with confirmation prompt |
| rm -f filename | Force delete without prompt |
| rm -r directory | Delete directory and contents recursively |
Key Takeaways
Use
rm filename to delete files permanently in Linux.Add
-i option to confirm before deleting to avoid mistakes.Be careful:
rm does not move files to trash; deletion is immediate.Use
rm -r to delete directories and their contents recursively.Always double-check file names before running
rm commands.