الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

rm

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

المستوى
امتحان RHCSA
المجموعة
الملفات والمجلدات
يحتاج root
لا
الحزمة
coreutils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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

طريقة الكتابة

rm [OPTION]... FILE...

الخيارات 12

الأكثر استخدامًا

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

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع 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.

انتبه

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

نصائح للحفظ

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