The rm
command in Linux is used to remove (delete) files and directories from the file system. It’s a powerful command that requires careful usage, as deleted files are not typically recoverable. Here’s how you can use the rm
command, along with some common options:
Basic Syntax
rm [options] file1 file2 ...
rm Command in Linux with examples
Commonly Used Options with rm
in Linux:
-i
: Interactive mode. The command prompts you for confirmation before deleting each file.Example:
rm -i file.txt
-f
: Force removal. This option overrides any prompts or warnings and forcefully removes files without asking for confirmation.Example:
rm -f file.txt
-r
or-R
: Recursive removal. This option is used to remove directories and their contents recursively.Example:
rm -r directory
-v
: Verbose mode. The command displays the names of the files as they are being removed.Example:
rm -v file1 file2
--
: This option indicates the end of options, which can be helpful when dealing with files that start with a hyphen (-
).Example:
rm -- -file.txt
Examples of Using rm
Command:
1. Remove a single file (with confirmation):
rm -i file.txt
2. Remove multiple files without confirmation (forceful removal):
rm -f file1.txt file2.txt
3. Remove a directory and its contents (recursively):
rm -r directory
4. Remove a directory and its contents without confirmation (forceful and recursive):
rm -rf directory
5. Remove files with verbose output:
rm -v file1.txt file2.txt
6. Remove a file with a name starting with a hyphen:
rm -- -file.txt
Caution: The rm command can be destructive, especially when used with the -r or -f options. Make sure you double-check the files and directories you intend to delete, as there is no easy way to recover them once they are deleted using this command.
Always exercise caution and consider using the -i option or carefully reviewing your command before using the -f option. Additionally, consider using safer alternatives like moving files to a trash directory (mv command) or using tools that provide more safety features when deleting files.