0
0
Linux CLIscripting~5 mins

rm (remove files) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to delete files you no longer want on your computer to keep things tidy or free up space. The rm command in Linux helps you remove files quickly from the command line.
When you want to delete a single file you created by mistake.
When you need to clean up old log files that are no longer needed.
When you want to remove temporary files after finishing a task.
When you want to delete multiple files at once to save time.
When you want to remove empty or unused directories along with their contents.
Commands
This command deletes the file named example.txt from the current folder. Use it when you want to remove a single file.
Terminal
rm example.txt
Expected OutputExpected
No output (command runs silently)
This command deletes example.txt but asks you to confirm before deleting. It helps prevent accidental removal.
Terminal
rm -i example.txt
Expected OutputExpected
rm: remove regular file 'example.txt'?
-i - Ask for confirmation before deleting each file
Deletes multiple files at once by listing them separated by spaces. Saves time when cleaning up several files.
Terminal
rm file1.txt file2.txt file3.txt
Expected OutputExpected
No output (command runs silently)
Deletes the folder named old_folder and everything inside it. Use this to remove directories with files.
Terminal
rm -r old_folder
Expected OutputExpected
No output (command runs silently)
-r - Recursively delete directories and their contents
Key Concept

If you remember nothing else from this pattern, remember: rm deletes files immediately and permanently, so use it carefully.

Common Mistakes
Running rm without checking the file names carefully
You might delete important files by accident because rm does not move files to trash.
Use rm -i to confirm each file before deletion or double-check file names before running rm.
Trying to delete a directory without the -r flag
rm will show an error because it cannot delete directories without recursive option.
Add the -r flag to delete directories and their contents.
Using rm -r on the wrong directory
This can delete many files and folders quickly, causing data loss.
Always double-check the directory name and path before running rm -r.
Summary
rm deletes files from the command line quickly and permanently.
Use rm -i to confirm before deleting files to avoid mistakes.
Use rm -r to delete directories and everything inside them.