Open In App

rm command in Linux with examples

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names). By default, it does not remove directories. This command normally works silently and you should be very careful while running rm command because once you delete the files then you are not able to recover the contents of files and directories. Syntax:

rm [OPTION]... FILE...

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
Removing one file at a time
$ rm a.txt

$ ls
b.txt c.txt d.txt e.txt

Removing more than one file at a time
$ rm b.txt c.txt

$ ls
d.txt e.txt

Note: No output is produced by rm, since it typically only generates messages in the case of an error. Options: 1. -i (Interactive Deletion): Like in cp, the -i option makes the command ask the user for confirmation before removing each file, you have to press y for confirm deletion, any other key leaves the file un-deleted.

$ rm -i d.txt
rm: remove regular empty file 'd.txt'? y

$ ls
e.txt

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
total 0
-r--r--r--+ 1 User User 0 Jan 2 22:56 e.txt

$ rm e.txt
rm: remove write-protected regular empty file 'e.txt'? n

$ ls
e.txt

$ rm -f e.txt

$ ls

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. Below is the tree of directories and files:

$ 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

rm command in Linux with examples – FAQs

What does the rm command do in Linux?

The rm (remove) command in Linux is used to delete files and directories from the filesystem. It is a powerful command that operates permanently, meaning it does not move files to a “trash” or “recycle bin” but removes them completely, making recovery difficult without the use of specialized forensic software. Because of its nature, it must be used with caution.

How to remove a single file using rm?

To remove a single file using the rm command, you simply specify the filename after the command. For example, to delete a file named example.txt, you would use:

rm example.txt

This command will permanently delete example.txt from the filesystem.

How to remove multiple files at once with rm?

To remove multiple files at once, you can list each filename separated by a space. For example, to delete file1.txt, file2.txt, and file3.txt simultaneously, you would use:You can also use wildcard characters to delete multiple files that match a pattern. For example, to delete all .txt files in the current directory, you can use:

rm file1.txt file2.txt file3.txt

Can rm be used to delete directories?

By default, rm does not delete directories, but it can be used to do so by adding the -r or --recursive option, which tells rm to recursively remove directories and their contents. For example, to delete a directory named docs along with all of its contents, you would use:

rm -r docs

What are some safety precautions when using rm?

Given the permanent nature of the rm command, several safety precautions are recommended:

1. Use interactive mode: You can use the -i option with rm to prompt for confirmation before each removal. This can prevent accidental deletion of files. For example:This will ask for your confirmation before deleting filename.txt.

rm -i filename.txt

2. Check with dry run: For recursive deletion, you might list files with ls first to see what’s in the directory or use the echo command to see which files rm would delete:Avoid running as superuser: Running rm as the root user can lead to disastrous results, especially if used improperly. Always double-check which user is executing the rm command.

3. Backup important data: Before running a risky rm operation, make sure important data is backed up in a different location.

4. Alias with safety nets: Some users alias rm to safer variants like rm -i to always prompt for confirmation. You can set this alias in your shell configuration file (e.g., .bashrc):

alias rm='rm -i'


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: