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

cat, tac

Print files to the screen, join files together (cat), or print them last line first (tac).

المستوى
امتحان RHCSA
المجموعة
النصوص والبحث
يحتاج root
لا
الحزمة
coreutils
يُسمّى أيضًا
tac

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

ماذا يفعل

cat (concatenate) writes the content of one or more files to standard output. With one file you read it; with several files you join them; with a redirect you create a new file.

It is perfect for short files. For long files, use less (scrolling) or head/tail (parts).

tac does the same, but starts with the last line (useful for logs: newest first).

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

cat [OPTION]... [FILE]...
tac [OPTION]... [FILE]...

الخيارات 15

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

-n, --number
Number all lines.
-b, --number-nonblank
Number only non-empty lines.
-A, --show-all
Show hidden characters: tabs as ^I, line ends as $, others as ^ or M- (same as -vET).
-E, --show-ends
Show $ at the end of each line.
-T, --show-tabs
Show tabs as ^I.
-v, --show-nonprinting
Show non-printable characters with ^ and M- notation.
-e
Same as -vE.
-t
Same as -vT.
-s, --squeeze-blank
Show several empty lines as one.
-u
Ignored.
--help
Show help.
--version
Show the version.

tac

-b, --before
Put the separator before the record instead of after.
-r, --regex
Treat the separator as a regular expression.
-s, --separator=string
Use this separator instead of a new line.

أمثلة 12

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Show a short file

    cat /etc/hostname
    server1.example.com

    The fastest way to read a small file.

  2. 2Show line numbers

    cat -n /etc/fstab

    Easy to talk about "line 7".

  3. 3Find hidden characters (tabs, Windows line ends)

    cat -A deploy.sh | head
    #!/bin/bash^M$

    ^M$ means Windows line ends: Bash will fail. ^I would show tabs (bad in YAML).

  4. 4Join several files into one

    cat part1.csv part2.csv part3.csv > all.csv

    The files are added one after the other.

  5. 5Create a file with several lines (here-document)

    cat > /tmp/test.conf <<'EOF'
    port=8080
    log=/var/log/app.log
    EOF

    Everything until the line EOF goes into the file.

  6. 6Add lines to a root-owned file

    cat <<'EOF' | sudo tee -a /etc/hosts
    192.168.122.20 server2.example.com server2
    EOF

    sudo tee -a appends as root.

  7. 7Show the newest log lines first

    tac /var/log/dnf.log | head -20

    The last 20 events, newest at the top.

  8. 8Show the OS version

    cat /etc/os-release

    NAME, VERSION_ID and ID of the distribution.

  9. 9Squeeze empty lines while reading

    cat -s notes.txt

    Several empty lines become one.

  10. 10Read kernel information

    cat /proc/cpuinfo | grep "model name" | head -1

    /proc files are created by the kernel when you read them.

  11. 11Show a file only if it exists

    [ -f /etc/motd ] && cat /etc/motd

    No error message in a script when the file is missing.

  12. 12Copy a disk image to a USB stick (as a simple dd)

    cat rhel-10.iso > /dev/sdX

    Works like dd for this job. Check the device name three times: it overwrites the whole disk.

انتبه

  • cat bigfile floods the screen; use less.
  • cat file > file empties the file before reading it. Never read and write the same file in one command.
  • "Useless use of cat": cat file | grep x works, but grep x file is simpler.

نصائح للحفظ

  • cat = show/join, tac = backwards, cat -A = invisible characters, cat > file <<'EOF' = new file.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man cat