Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

rm

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

Niveau
RHCSA-examen
Groep
Bestanden en mappen
Root nodig
Nee
Pakket
coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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).

Zo schrijf je het

rm [OPTION]... FILE...

Opties 12

Meest gebruikt

-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.

Voorbeelden 11

$ als gewone gebruiker# als root (of met 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.

Let op

  • 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 om te onthouden

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