The rm
(remove) command is used to delete files and directories in a Linux filesystem.
-
Syntax:
rm [options] [file...]
-
Examples:
rm file.txt
- Removes the file named
file.txt
.
rm -r directory
- Recursively removes the directory and its contents.
rm -f file.txt
- Forcibly removes
file.txt
without prompting, even if it's write-protected.
rm -i file.txt
- Prompts for confirmation before removing
file.txt
.
rm -v file.txt
- Verbosely shows what is being removed.
- Removes the file named
-
-r
or--recursive
:- Removes directories and their contents recursively.
rm -r folder
- Removes the
folder
directory and everything inside it.
-
-f
or--force
:- Forces removal of files without prompting, even if the file is write-protected.
rm -f important.txt
- Removes
important.txt
without any confirmation.
-
-i
or--interactive
:- Prompts for confirmation before each file is removed.
rm -i file.txt
- Asks for confirmation before deleting
file.txt
.
-
-v
or--verbose
:- Shows what is being removed.
rm -v file.txt
- Outputs a message that
file.txt
is being deleted.
-
--help
:- Displays help information about the
rm
command.
rm --help
- Shows usage information and options.
- Displays help information about the
-
Be Cautious:
- The
rm
command permanently deletes files and directories. There is no undo, so use it carefully, especially with options like-r
and-f
.
- The
-
Removing Multiple Files:
- You can remove multiple files at once by listing them after
rm
.
rm file1.txt file2.txt
- You can remove multiple files at once by listing them after
-
Wildcard Usage:
- Use wildcards to remove multiple files that match a pattern.
rm *.txt
- Removes all
.txt
files in the current directory.
The rm
command is powerful for removing files and directories in Linux. Use it with options like -r
, -f
, and -i
to control how files and directories are deleted. Always double-check what you're removing, especially when using recursive or forced deletion.