Cheat Sheet #day48 - rm

Cloud TunedCloud Tuned
2 min read

rm Command Cheatsheet

The rm command in Unix/Linux is used to remove files and directories. It stands for "remove" and can be a powerful tool when used with caution. Below are the basic and advanced usages of the rm command along with practical examples.

Basic Usage

  • Remove a file

      rm filename
    
  • Remove multiple files

      rm file1 file2 file3
    

Common Options

  • Interactive mode (prompt before every removal)

      rm -i filename
    
  • Force removal (ignore nonexistent files and never prompt)

      rm -f filename
    
  • Recursive removal (remove directories and their contents)

      rm -r directoryname
    
  • Verbose mode (explain what is being done)

      rm -v filename
    
  • Remove directories (same as -r)

      rm -d directoryname
    

Examples

  • Remove a single file

      rm file.txt
    
  • Remove multiple files

      rm file1.txt file2.txt file3.txt
    
  • Remove a directory and its contents

      rm -r directoryname
    
  • Interactive removal

      rm -i file.txt
    
  • Force removal of a file

      rm -f file.txt
    
  • Verbose removal

      rm -v file.txt
    

Advanced Usage

  • Remove all files with a specific extension

      rm *.txt
    
  • Remove files using a wildcard

      rm file*
    
  • Remove empty directories

      rm -d directoryname
    
  • Forcefully remove a directory and its contents

      rm -rf directoryname
    

Practical Tips

  • Safety First: Use the -i option to prevent accidental deletions.

  • Force Removal: Use the -f option to force the removal of files without prompts, but use it cautiously.

  • Recursive Removal: Always use -r or -rf with caution, especially when running as a root user, to avoid deleting critical system files.

  • Verbose Mode: Use -v to see exactly what rm is doing, useful for debugging scripts.

Quick Reference

  • Remove a file:

      rm filename
    
  • Remove multiple files:

      rm file1 file2 file3
    
  • Interactive mode:

      rm -i filename
    
  • Force removal:

      rm -f filename
    
  • Recursive removal:

      rm -r directoryname
    
  • Verbose mode:

      rm -v filename
    
  • Remove directories:

      rm -d directoryname
    
  • Forcefully remove a directory and its contents:

      rm -rf directoryname
    

This cheatsheet covers the essential commands and options for using rm effectively, from basic file deletions to more advanced directory management tasks. Adjust the commands according to your specific requirements and environment.

0
Subscribe to my newsletter

Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cloud Tuned
Cloud Tuned