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.txtwithout 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
-
-ror--recursive:- Removes directories and their contents recursively.
rm -r folder
- Removes the
folderdirectory and everything inside it.
-
-for--force:- Forces removal of files without prompting, even if the file is write-protected.
rm -f important.txt
- Removes
important.txtwithout any confirmation.
-
-ior--interactive:- Prompts for confirmation before each file is removed.
rm -i file.txt
- Asks for confirmation before deleting
file.txt.
-
-vor--verbose:- Shows what is being removed.
rm -v file.txt
- Outputs a message that
file.txtis being deleted.
-
--help:- Displays help information about the
rmcommand.
rm --help
- Shows usage information and options.
- Displays help information about the
-
Be Cautious:
- The
rmcommand permanently deletes files and directories. There is no undo, so use it carefully, especially with options like-rand-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
.txtfiles 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.