Linux rm command guide remove files
Command definition
The rm
command in Linux is used to remove files or directories from the filesystem. It is a powerful command that deletes files permanently without moving them to a trash or recycle bin, making it essential to use it with caution. The command can also be combined with various options to modify its behavior, such as removing directories recursively or forcing the deletion of write-protected files.
Usage examples
-
Remove a single file: This command deletes a specified file from the filesystem.
Example:rm filename.txt
-
Remove multiple files: This command allows you to delete several files at once by listing them.
Example:rm file1.txt file2.txt file3.txt
-
Force removal of files: This option forces the deletion of files without prompting for confirmation, even if they are write-protected.
Example:rm -f filename.txt
-
Remove a directory: This command deletes an empty directory.
Example:rm myfolder
-
Remove a directory and its contents: This option allows you to delete a directory and all its files and subdirectories recursively.
Example:rm -r myfolder
-
Force removal of a directory and its contents: This command combines the force and recursive options to delete a directory and its contents without confirmation.
Example:rm -rf myfolder
-
Prompt before removal: This option prompts the user for confirmation before deleting each file.
Example:rm -i filename.txt
-
Remove files based on a pattern: This command uses wildcards to delete files that match a specific pattern.
Example:rm *.txt
(removes all.txt
files in the current directory) -
Remove files with a specific extension: This command deletes files with a particular extension in the current directory.
Example:rm -f *.log
(removes all.log
files) -
Remove files interactively: This command allows users to confirm each file deletion individually, which is safer for critical files.
Example:rm -i file1.txt file2.txt