Deleting with rm

rm deletes files and directories — permanently, with no recycle bin.

Syntaxrm [options] file...

rm removes files. Be careful: there is no trash and no undo. A deleted file is gone.

Options

  • -r — recursive: delete a directory and everything in it.
  • -i — interactive: ask before each deletion.
  • -f — force: never prompt, ignore missing files.

To remove an empty directory you can also use rmdir, which refuses to delete anything that still contains files — a small built-in safety net.

Example

Example · bash
# Delete a single file
rm old.txt

# Delete several files
rm a.txt b.txt c.txt

# Delete a folder and all its contents
rm -r build/

# Remove an empty directory safely
rmdir emptyfolder

When to use it

  • A CI job runs 'rm -rf ./dist' to clean up a stale build directory before a fresh build.
  • A developer deletes all '.pyc' files in a project with 'rm **/*.pyc' to remove cached bytecode.
  • A sysadmin uses 'rm -i /tmp/*.sql' to interactively confirm before deleting each SQL dump file.

More examples

Delete files safely

Shows basic file deletion and -i mode which asks for confirmation, preventing accidental removal.

Example · bash
# Delete a single file
rm temp.txt

# Interactive mode - prompts before each deletion
rm -i *.log

Remove directory recursively

Contrasts rmdir (empty only) with 'rm -r' for directories containing files, and adds -f for scripting.

Example · bash
# Remove an empty directory
rmdir empty_dir/

# Remove directory and all contents
rm -r old_project/

# Force-remove without prompts (use with caution)
rm -rf build/

Delete files matching a pattern

Uses glob patterns and 'find -delete' to safely target files by name pattern or age.

Example · bash
# Remove all .tmp files in current directory
rm *.tmp

# Remove files older than 7 days in /tmp
find /tmp -maxdepth 1 -mtime +7 -type f -delete

Discussion

  • Be the first to comment on this lesson.