Back to Blog

Deleting Files and Directories | rm & rmdir Commands | Mac/Linux Terminal

Sandy LaneSandy Lane

Video: Deleting Files and Directories | rm & rmdir Commands | Mac/Linux Terminal by Taught by Celeste AI - AI Coding Coach

Watch full page →

Deleting Files and Directories with rm and rmdir Commands on Mac/Linux

Learn how to safely delete files and directories in Mac and Linux terminals using the powerful rm and rmdir commands. This guide covers deleting single or multiple files, removing empty directories, and performing recursive deletions with safety tips to avoid accidental data loss.

Code

# Delete a single file permanently
rm filename.txt

# Delete multiple files at once
rm file1.txt file2.txt file3.txt

# Use interactive mode to confirm before deleting each file
rm -i file1.txt file2.txt

# Use verbose mode to see each file as it is deleted
rm -v file1.txt file2.txt

# Remove an empty directory
rmdir empty_directory

# Remove multiple empty directories
rmdir dir1 dir2 dir3

# Attempting to remove a non-empty directory with rmdir will fail
rmdir non_empty_directory
# Output: rmdir: failed to remove 'non_empty_directory': Directory not empty

# Recursively delete a directory and all its contents (use with caution!)
rm -r directory_name

# Recursive deletion with interactive prompts for safety
rm -ri directory_name

# Recursive deletion with verbose output
rm -rv directory_name

# Force recursive deletion without prompts (dangerous!)
rm -rf directory_name

Key Points

  • rm permanently deletes files and should be used carefully as there is no undo or trash recovery.
  • rm -i enables interactive mode to confirm each deletion, reducing accidental removals.
  • rmdir only removes empty directories and will error if the directory contains files or subdirectories.
  • Use rm -r to recursively delete directories and all their contents, but always double-check paths to avoid data loss.
  • Combining recursive deletion with interactive mode (rm -ri) is a safer practice for removing directories.