The rm command in Unix and Linux is used to remove files or directories. It is a powerful command that can delete files permanently, so it must be used with caution. The command has various options that provide flexibility in how files and directories are deleted.
Basic Usage
The basic syntax for the rm command is:
rm [options] file...
options: Command-line options to control the behavior of rm.
file: The file(s) or directory(ies) to be removed.
Examples
Removing a Single File
To remove a single file:
rmfile.txt
This command deletes file.txt. If file.txt doesn't exist, an error message is displayed.
Removing Multiple Files
To remove multiple files:
rmfile1.txtfile2.txt
This command deletes file1.txt and file2.txt.
Removing an Empty Directory
To remove an empty directory:
rmdirdir
This command deletes the empty directory dir.
Options
-i Option: Interactive Mode
To prompt for confirmation before each removal:
rm-ifile1.txtfile2.txt
This command asks for confirmation before deleting each file.
-f Option: Force Mode
To force removal of files without prompting:
rm-ffile.txt
This command deletes file.txt without any confirmation, even if it is write-protected.
-r Option: Recursive Removal
To remove directories and their contents recursively:
rm-rdir
This command deletes the directory dir and all its contents, including subdirectories.
-v Option: Verbose Mode
To display detailed information about what is being removed:
rm-vfile1.txtfile2.txt
This command outputs detailed information about the files being deleted.
Practical Use Cases
Deleting Files in a Directory
To delete all files in a directory:
rmdir/*
This command deletes all files in the dir directory, but not the directory itself.
Removing a Directory and Its Contents
To remove a directory and all its contents:
rm-rdir
This command deletes the directory dir and all files and subdirectories within it.
Forcing Deletion of Write-Protected Files
To delete a write-protected file without prompting:
rm-ffile.txt
This command forces the deletion of file.txt, even if it is write-protected.
Safety Tips
Use rm with caution: Files deleted with rm cannot be easily recovered.
Double-check file paths: Ensure you specify the correct files or directories to avoid accidental data loss.
Use interactive mode (-i): When in doubt, use the -i option to prompt for confirmation before each deletion.
Summary
The rm command is a powerful tool for removing files and directories in Unix and Linux environments. Its various options provide flexibility for different deletion scenarios, such as recursive removal, force deletion, and interactive mode. Understanding these options and practical use cases can help you use rm safely and effectively.
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[=all] do not remove '/' (default);
with 'all', reject any command line argument
on a separate device from its parent
-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
man
NAME
rm - remove files or directories
SYNOPSIS
rm [OPTION]... [FILE]...
DESCRIPTION
This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories.
If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given,
then rm prompts the user for whether to proceed with the entire operation. If the response is not affirmative, the entire com‐
mand is aborted.
Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --in‐
teractive=always option is given, rm prompts the user for whether to remove the file. If the response is not affirmative, the
file is skipped.
OPTIONS
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 giv‐
ing 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 correspond‐
ing command line argument
--no-preserve-root
do not treat '/' specially
--preserve-root[=all]
do not remove '/' (default); with 'all', reject any command line argument on a separate device from its parent
-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.