Open In App

rm command in Linux with examples

Last Updated : 25 Oct, 2025
Comments
Improve
Suggest changes
14 Likes
Like
Report

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.

Example

  • 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
1
Create file

1. Deleting a Single File

  • First we delete the single file
rm a.txt
ls
1
remove a single file

2. Deleting Multiple Files

  • Now we delete the multiple files using rm command
rm b.txt c.txt
ls
1
Deleting Multiple Files

Note: 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
1
using -i option in rm command

2. ' -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

1
using -f option in rm command

Note: -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


Explore