The rm
command stands for “remove.” It is used to delete files and directories from the file system. Unlike the rmdir
command, which only removes empty directories, rm
can remove both files and directories regardless of whether they contain other files or directories.
Initial Example
Using rm
to remove a file called example.txt
:
rm example.txt
Warning! Avoid using -r and -f unless necessary. Instead, add -i option to prevent accidental deletion.
RM Parameters
Parameter | Description |
---|---|
-f, --force | Remove files and directories without confirmation, ignoring nonexistent files |
-i | Prompt for confirmation before each file or directory removal |
-I | Prompt once before removing more than three files or when removing directories recursively |
--interactive[=WHEN] | Set the prompt behavior based on WHEN: never , once (-I), or always (-i); defaults to always |
--one-file-system | When removing directories recursively, avoid directories on different file systems |
--no-preserve-root | Allow deletion of the root directory (/ ), which is usually protected by default |
--preserve-root[=all] | Prevent removal of the root directory (/ ); with all , protect directories on different devices |
-r, -R, --recursive | Recursively delete directories and their contents |
-d, --dir | Remove empty directories only |
-v, --verbose | Display detailed information about the removal process |
--help | Show help information and exit |
--version | Display version information and exit |
Examples
1. Remove a Single File
To delete a single file:
rm filename.txt
2. Remove Multiple Files
To delete multiple files at once, specify their names separated by spaces:
rm file1.txt file2.txt file3.txt
3. Remove a Directory and Its Contents
To remove a directory and all its contents (files and subdirectories), use the -r
(recursive) option:
rm -r directory_name
4. Force Remove Files or Directories
To forcefully remove files or directories without prompting for confirmation, use the -f
option:
rm -f filename.txt rm -rf directory_name
5. Interactive Deletion
To prompt for confirmation before deleting each file, use the -i
option:
rm -i filename.txt rm -r -i directory_name