This guide will walk you through the basics to advanced usage of the ‘rm’ command in Linux. We’ll cover everything from the simple deletion of files to more complex tasks, such as recursive deletion and forced deletion.
How to Basic Use of ‘rm’ Command in Ubuntu
Delete a file in Linux, you use the ‘rm’ command followed by the filename, for the complete syntax, rm [arguments] file
. This command is your go-to tool for removing files or directories in your Linux filesystem.
rm file.txt
# Output:
# file.txt is deleted
That’s the basic use of the ‘rm’ command. It’s straightforward, but powerful. However, it’s important to note that using the ‘rm’ command is irreversible.
How to Advanced Use of ‘rm’ Command
These flags can make the ‘rm’ command more powerful and flexible, allowing you to handle more complex file and directory deletion tasks.
Flag | Description | Example |
---|---|---|
-r or -R | Recursive removal. Allows the deletion of directories and their contents. | rm -r directory |
-f | Force deletion. Ignores nonexistent files and arguments, and never prompts. | rm -f filename |
-i | Interactive prompt before every removal. | rm -i filename |
-v | Verbose. Explains what is being done. | rm -v filename |
-d | Remove directories without prompting for confirmation. | rm -d directory |
--preserve-root | Do not remove ‘/’ (default). | rm --preserve-root |
--no-preserve-root | Do not treat ‘/’ specially. | rm --no-preserve-root |
--one-file-system | Skip directories on different file systems. | rm --one-file-system |
-I | Prompt once before removing more than three files, or when removing recursively. | rm -I |
-- | End of options. | rm -- -filename |
Example
rm -rf /var/www/domain.com/my-directory
Delete my-directory recursive removal and never prompts.