The rm
command in Linux is used to remove or delete files and directories from the filesystem. It’s one of the most fundamental and potentially dangerous commands because deleted files cannot be easily recovered. The name rm
is short for “remove,” and the command permanently deletes files rather than moving them to a trash or recycle bin.
Contents
Basic File Deletion
# Delete a single file
rm filename.txt
# Delete multiple files
rm file1.txt file2.txt file3.txt
# Delete all text files in current directory
rm *.txt
Confirmation and Verbose Options
# Ask for confirmation before deleting
rm -i important_file.txt
# Show verbose output (what's being deleted)
rm -v document.pdf
Deleting Directories
# Delete an empty directory
rm -d empty_directory/
# Delete a directory and all its contents recursively
rm -r documents/
# Force delete a directory and all contents without prompts
rm -rf old_project/
Advanced Usage
# Delete files with special characters in names
rm -f "file with spaces.txt"
rm -f file\ with\ spaces.txt
# Delete files with names starting with a dash
rm -- -oddfile.txt
# Delete files but preserve specific ones (using find)
find . -name "*.log" -not -name "system.log" -exec rm {} \;
Safety Options
# Interactive mode for wildcards (asks for each file)
rm -i *.conf
# Prompt only once before removing more than 3 files
rm -I *.jpg
# Do a dry run (no actual deletion) with verbose output
rm -v --dry-run *.tmp
Handling Write-Protected Files
# Delete write-protected files (overrides file permissions)
rm -f write_protected.txt
# Prompt before removing write-protected files
rm -i protected_files/*
Remember to use the rm
command with caution, especially when combined with wildcards or the -r
and -f
options, as deleted files cannot be easily recovered unless you have a backup system in place.