Table of Contents
ToggleMastering the rm
Command in Linux: How to Delete Files and Directories Safely
If you’re working on a Linux or Unix-based system, knowing how to delete files and directories using the rm
command is essential. In this tutorial, we’ll explore how the rm
command works, how to safely remove files and directories, and how to use its most useful options like -i
, -f
, -v
, and -r
.
Whether you’re a beginner or just need a refresher, this guide will walk you through everything you need to know about using rm
.
What is the rm
Command in Linux?
The rm
(short for remove) command in Linux is used to delete files and directories. By default, it does not move files to the trash—they are permanently removed, so it’s important to use this command carefully.
Creating Sample Files to Test
Let’s begin by creating some test files using the touch
command:
touch 1.txt 2.txt 3.txt 4.txt
ls
This will create four text files. Use ls
to confirm the files are created.
Basic Usage of rm
to Delete Files
To delete a single file:
rm 1.txt
After running this, use ls
again and you’ll see that 1.txt
is gone.
To delete multiple files in one go:
rm 2.txt 3.txt 4.txt
This command will remove all the listed files at once.
Using rm -i
: Confirm Before Deleting
The -i
(interactive) flag prompts you before deleting each file. This is a safety measure to prevent accidental deletions:
rm -i 1.txt
You’ll get a prompt:
remove regular empty file ‘1.txt’?
Type y
to confirm deletion or n
to skip it.
Using rm -I
: Confirm Before Bulk Deletion
The capital -I
option is similar to -i
, but it only prompts once if you’re deleting more than three files:

rm -I 1.txt 2.txt 3.txt 4.txt
Prompt:
rm: remove 4 arguments?
Type y
to confirm all deletions.
Using rm -v
: Show What’s Being Deleted
The -v
(verbose) flag shows which files or directories are being deleted:
rm -v 1.txt 2.txt 3.txt 4.txt
Output:
removed '1.txt'
removed '2.txt'
removed '3.txt'
removed '4.txt'
This is useful for tracking deletion in scripts or logs.
Deleting Directories Using rm -r
To delete non-empty directories, use the -r
(recursive) flag:
rm -r directory_name
For example, if you create nested directories:
mkdir -p a1/a2/a3
Then delete them recursively:
rm -r a1
This will remove a3
, a2
, and a1
in sequence.
Using rm -ir
: Safe Recursive Deletion
Add -i
to get confirmation at each step:
rm -ir a1
You’ll be prompted to confirm each directory deletion (a3, a2, and a1).
Using rm -rf
: Forceful Deletion
To delete directories without any prompts (use with extreme caution):
rm -rf a1
This will delete everything inside a1
without any confirmation.
Deleting Empty Directories with rm -d
If you only want to delete an empty directory:
rm -d s00
For verbose output:
rm -dv s00
Summary: Common rm
Command Options
Option | Description |
---|---|
-i |
Prompts before every deletion |
-I |
Prompts once if deleting multiple files |
-r |
Recursively delete directories |
-f |
Force delete, no prompts |
-v |
Verbose output showing deleted files |
-d |
Remove empty directories only |
Final Thoughts
The rm
command is a powerful Linux tool that should be used with care. Whether you’re cleaning up files or managing nested directories, mastering its options like -i
, -r
, and -v
will help you avoid mistakes and work more efficiently.
If you found this tutorial helpful, feel free to share it and bookmark it for future reference.
Need more Linux tips? Check out our full Linux Command Line Tutorial Series to deepen your terminal skills!