rm command in Linux with examples
Last Updated :
01 Nov, 2025
The rm (remove) command in Linux is used to delete files and directories from the file system permanently.
- By default,
rm removes files without moving them to any recycle bin — the deletion is irreversible. - To remove directories and their contents, use the
-r (recursive) option. - The
-f (force) option can be used to suppress confirmation prompts and ignore non-existent files. - It’s a powerful command - users should use it with caution to avoid accidental data loss.
Let us consider 5 files having name a.txt, b.txt and so on till e.txt
$ ls
a.txt b.txt c.txt d.txt e.txt
Create file1. Deleting a Single File
- First we delete the single file
rm a.txt
ls
remove a single file2. Deleting Multiple Files
- Now we delete the multiple files using rm command
rm b.txt c.txt
ls
Deleting Multiple FilesNote: No output is produced by rm, since it typically only generates messages in the case of an error.
Options Available in rm Command
Here are the most commonly used and practical options of the rm command in Linux:
Syntax:
rm [OPTION]... FILE...
1. ' -i ' (Interactive Deletion)
The -i option makes the rm command interactive, prompting for confirmation before deleting each file.
rm -i d.txt
using -i option in rm command2. ' -f ' (Force Deletion)
rm prompts for confirmation removal if a file is write protected. The -f option overrides this minor protection and removes the file forcefully.
ls -l
rm e.txt # It shows rm: remove write-protected regular empty file 'e.txt'?
rm -f e.txt
using -f option in rm commandNote: -f option of rm command will not work for write-protect directories. 3. -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will delete all the files and sub-directories recursively of the parent directory. At each stage it deletes everything it finds. Normally, rm wouldn't delete the directories but when used with this option, it will delete.
Recursive Deletion (-r or -R)
Deletes directories and their contents recursively:
$ ls
A
$ cd A
$ ls
B C
$ ls B
a.txt b.txt
$ ls C
c.txt d.txt
Now, deletion from A directory(as parent directory) will be done as:
$ rm *
rm: cannot remove 'B': Is a directory
rm: cannot remove 'C': Is a directory
$ rm -r *
$ ls
Every directory and file inside A directory is deleted. 4. --version: This option is used to display the version of rm which is currently running on your system.
$ rm --version
rm (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Paul Rubin, David MacKenzie, Richard M. Stallman,
and Jim Meyering.
Applications of wc Command Delete file whose name starting with a hyphen symbol (-):
To remove a file whose name begins with a dash ("-"), you can specify a double dash ("--") separately before the file name. This extra dash is necessary so that rm does not misinterpret the file name as an option. Let say there is a file name -file.txt, to delete this file write command as:
$ ls
-file.txt
$ rm -file.txt
rm: unknown option -- l
Try 'rm ./-file.txt' to remove the file '-file.txt'.
Try 'rm --help' for more information.
$ rm -- -file.txt
$ ls
?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L
What is the default behavior of rm when deleting files?
-
-
Sends them to recovery storage
-
-
Explanation:
rm permanently removes files without sending them to any recycle bin.
Which option allows deleting directories along with all their contents?
Explanation:
rm -r enables recursive deletion, removing directories and everything inside them.
Which command deletes files without showing any confirmation prompt, even if write-protected?
Explanation:
rm -f forces deletion with no prompts and ignores write protection.
Which option is used to safely delete a file whose name begins with a hyphen (–)?
Explanation:
Using -- tells rm to stop option parsing and treat -file.txt as a filename.
What is the key difference between rm and rmdir?
-
rm deletes only empty directories; rmdir deletes files
-
rmdir can delete system folders; rm cannot
-
rm deletes files and directories; rmdir deletes only empty directories
-
Both commands work identically
Explanation:
rm deletes files and, with -r, full directories; rmdir deletes only empty directories.
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Getting Started with Linux
Installation with Linux
Linux Commands
Linux File System
Linux Kernel
Linux Networking Tools
Linux Process
Linux Firewall
Shell Scripting & Bash Scripting
Linux Administrator System