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.
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.
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 their 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
- scp command in Linux with Examples
- rcp Command in Linux with examples
- ssh command in Linux with Examples
- cmp Command in Linux with examples
- for command in Linux with Examples
- Cat command in Linux with examples
- less command in Linux with Examples
- gdb command in Linux with examples
- cpp command in Linux with Examples
- last command in Linux with Examples
- cd command in Linux with Examples
- mv command in Linux with examples
- seq command in Linux with Examples
- arp command in Linux with examples
- apt command in linux with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.