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

wc

Count lines, words, characters and bytes.

المستوى
امتحان RHCSA
المجموعة
النصوص والبحث
يحتاج root
لا
الحزمة
coreutils

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

ماذا يفعل

wc (word count) prints, for each file: the number of lines, words and bytes. With options you choose one count. wc -l is used all the time to count lines of a file or results of a command.

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

wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F

الخيارات 9

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

-l, --lines
Count lines (new line characters).
-w, --words
Count words (separated by spaces).
-c, --bytes
Count bytes.
-m, --chars
Count characters (different from bytes for UTF-8 text).
-L, --max-line-length
Print the length of the longest line.
--total=when
When to print the total line: auto, always, only, never.
--files0-from=file
Read file names from a file (NUL separated).
--help
Show help.
--version
Show the version.

أمثلة 11

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

  1. 1Lines, words and bytes

    wc /etc/passwd
      45   95 2512 /etc/passwd

    Three numbers: lines, words, bytes.

  2. 2Count lines only

    wc -l /etc/passwd
    45 /etc/passwd

    Each line is one account.

  3. 3Count results of a command

    ls /etc | wc -l

    How many entries in /etc.

  4. 4Only the number, without the file name

    wc -l < /etc/passwd
    45

    Input from a redirect has no name. Good in scripts.

  5. 5Count running processes

    ps -e --no-headers | wc -l

    How many processes run now.

  6. 6Count failed SSH logins today

    journalctl -u sshd --since today | grep -c "Failed password"

    grep -c counts directly; same as grep … | wc -l.

  7. 7Count lines of all scripts in a folder

    wc -l *.sh

    Each file plus a total line.

  8. 8Check the longest line of a file

    wc -L config.yaml

    Some tools have line length limits.

  9. 9Count characters in UTF-8 text

    echo -n "سلام" | wc -m
    4

    -c would count 8 bytes; -m counts 4 characters.

  10. 10Compare the size of two lists

    wc -l before.txt after.txt

    Quick check after a cleanup or an import.

  11. 11Alert when a log grows too fast

    [ "$(wc -l < /var/log/app/error.log)" -gt 1000 ] && echo "too many errors"

    A tiny monitoring check.

انتبه

  • The last line of a file without a new line is not counted by -l.

نصائح للحفظ

  • wc -l = how many lines. wc -l < file = number only.