In this article, I will take you through 12 Most Popular rm command in Linux with examples. Sometimes you might get asked what is the Linux command for removing a directory or what command do you use to delete a file. Answers of all these questions is rm linux command.
You might be aware of rm linux command as this is the most basic command we use to delete files and directories from command line.
What does rm stand for in Linux
rm stand for remove. It basically removes files and directories in Linux.
rm command in Linux with examples
Example 1: Check rm linux command version
You can check rm command version by using
--version option as shown below. As you can check from below output, current version is
[root@localhost ~]# rm --version rm (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. 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.
--version : output version information and exit
Example 2: Remove a File Using rm command
You can simply remove a file
download.txt by using rm command as shown below. Please note that by default it will ask you for confirmation before removing the file.
[root@localhost ~]# rm download.txt rm: remove regular file ‘download.txt’? y
Example 3: Using rm options -i to interactively delete file
You can use rm flag
-i to interactively delete the file as shown below. By default this option will be enabled.
[root@localhost ~]# rm -i file.txt rm: remove regular file ‘file.txt’? y
-i : prompt before every removal
Example 4: Using rm options -f to delete a file without confirmation
If you do not want rm command to ask for a confirmation before deleting a file you can use
-f option to forcefully delete it as shown below.
[root@localhost ~]# rm -f file.txt
-f : ignore nonexistent files and arguments, never prompt
Example 5: Using rm command option -v to verbose the output
If you want to see the output of rm command then you need to use
-v option to show the output as mentioned in below example.
[root@localhost ~]# rm -v file1.txt rm: remove regular empty file ‘file1.txt’? y removed ‘file1.txt’
-v : explain what is being done
Example 6: Remove an empty directory without using rm command -d flag
You can simply delete empty directory by using
rm -rvf hello command as shown below.
[root@localhost ~]# rm -rvf hello removed directory: ‘hello’
-r : remove directories and their contents recursively
Example 7: Using rm options -r in Linux to delete files recursively along with directory
If you want to recursively delete all the files and directory along with its sub-directories then you need to use
-r flag with rm command as shown below.
[root@localhost ~]# rm -rvf example removed ‘example/file.txt’ removed ‘example/file2.txt’ removed directory: ‘example’
Example 8: Using rm command in Linux to remove everything from current directory
You can also use wildcard(*) character to delete all the files and directories. In this example, we are trying to delete all files and sub directories from hello directory as shown below.
[root@localhost hello]# rm -vf * removed ‘file1.txt’ removed ‘file2.txt’ removed ‘file.txt’
Example 9 : Remove Empty Directories
If you want to remove empty directories then you need to use
-d flag with rm command in Linux as shown below.
[root@localhost ~]# rm -vd hello rm: remove directory ‘hello’? y removed directory: ‘hello’
-d : Remove empty directories
If directory is not empty then it will throw below error.
[root@localhost ~]# rm -vd hello rm: cannot remove ‘hello’: Directory not empty
Example 10 : Preserve root file system
If you do not want to recursively delete everything from root(/) filesystem then you need to use
--preserve-root option as shown below.
[root@localhost ~]# rm -rf --preserve-root /
By default it will run with
[root@localhost ~]# rm -rf / rm: it is dangerous to operate recursively on ‘/’ rm: use --no-preserve-root to override this failsafe
--preserve-root : do not remove '/'
Example 11 : Do not preserve root filesystem
If you want to recursively delete everything from root(/) filesystem then you need to use
--no-preserve-root option with rm command as shown below.
[root@localhost ~]# rm -rf --no-preserve-root /
--no-preserve-root : do not treat '/' specially
Example 12. Check Other Options with --help
You can check all the other rm options that can be used using
--help flag as shown below.
[root@localhost ~]# rm --help Usage: rm [OPTION]... FILE... Remove (unlink) the FILE(s). -f, --force ignore nonexistent files and arguments, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always --one-file-system when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument --no-preserve-root do not treat '/' specially --preserve-root do not remove '/' (default) -r, -R, --recursive remove directories and their contents recursively -d, --dir remove empty directories -v, --verbose explain what is being done --help display this help and exit --version output version information and exit By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents. To remove a file whose name starts with a '-', for example '-foo', use one of these commands: rm -- -foo rm ./-foo Note that if you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time. For greater assurance that the contents are truly unrecoverable, consider using shred. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'rm invocation'
--help : display this help and exit