Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

rm

Delete files, and with -r folders with all their content.

Level
RHCSA exam
Group
Files and folders
Needs root
No
Package
coreutils

What it does

rm removes file names. When the last name of a file is gone (and no program has it open), the space is free. There is no trash can: a removed file is gone.

rm does not remove folders unless you add -r. -f removes without questions and without errors for missing files.

On RHEL, root has the alias rm='rm -i', so it asks for each file. rm also refuses to remove / itself (--preserve-root).

How to write it

rm [OPTION]... FILE...

Options 12

Most used

-r, -R, --recursive
Remove folders and everything in them.
-f, --force
Never ask, ignore files that do not exist.
-i
Ask before every removal.
-I
Ask once before removing more than three files or a whole folder (less noisy than -i).
--interactive[=<when>]
When to ask: never, once (like -I) or always (like -i).
-d, --dir
Remove empty folders too (like rmdir).
-v, --verbose
Print each file removed.
--one-file-system
With -r: skip folders on other file systems.
--preserve-root[=all]
Do not remove / (the default). With "all": also refuse mount points on other devices.
--no-preserve-root
Allow removing /. Never use this.
--help
Show help.
--version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Remove a file

    rm notes.txt

    The file is gone; no undo.

  2. 2Remove a folder with its content

    rm -r /tmp/build

    Asks for each file when the rm -i alias is active (root on RHEL).

  3. 3Remove without any question

    rm -rf /tmp/build

    Powerful and dangerous. Check the path twice.

  4. 4Ask once for a big removal

    rm -rI ~/old-projects

    One question instead of one per file.

  5. 5Remove files with a name that starts with a dash

    rm -- -weird-name.txt

    -- means: the options end here.

  6. 6Remove old logs with find (safer than wildcards)

    find /var/log/myapp -name "*.log" -mtime +30 -print -delete

    Removes logs older than 30 days and prints each name.

  7. 7Remove a file with spaces in its name

    rm "My Report (final).docx"

    Quotes keep the name together.

  8. 8Test a wildcard before removing

    echo /tmp/cache/*.tmp
    rm /tmp/cache/*.tmp

    echo shows exactly what the shell will pass to rm.

  9. 9Free space that a deleted log still uses

    lsof +L1 | grep deleted
    systemctl restart rsyslog

    A program that keeps a deleted file open still uses the space. Restart it to free the space.

  10. 10Stay on one file system

    rm -rf --one-file-system /mnt/oldroot/tmp

    Protects mounted file systems below the folder from being wiped.

  11. 11Remove empty folders and files in one go

    rm -dv empty-dir file.txt

    -d removes the empty folder too.

Watch out

  • No trash, no undo. Make a copy first or use mv to a temp folder when you are unsure.
  • Never put a variable alone in rm: rm -rf "$DIR/"* with an empty DIR deletes from /. Use ${DIR:?}.
  • A space in the wrong place: rm -rf / tmp/x deletes everything. Read the line before Enter.

Tips to remember

  • File: rm. Folder: rm -r. No questions: -f. One question: -I.
  • In scripts: rm -rf "${DIR:?}/"* stops when DIR is empty.