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

wc

Count lines, words, characters and bytes.

Level
RHCSA exam
Group
Text and search
Needs root
No
Package
coreutils

What it does

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.

How to write it

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

Options 9

Most used

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

Examples 11

$ run as a normal user# run as root (or with 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.

Watch out

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

Tips to remember

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