Skip to content
Log In Sign Up info@muvcloud.com

The wc Commands You Will Use Most

wc -l /var/log/nginx/access.log
wc -l < /var/log/nginx/access.log
grep " 404 " /var/log/nginx/access.log | wc -l
find /var/www/uploads -type f | wc -l

The first command prints the number of lines followed by the file name. The second reads the file from standard input, so only the number is printed, which is what you want in scripts. The last two count the lines of another command's output: requests with status 404, and files in a folder.

All Counts at Once

wc notes.txt

Without options, wc prints three numbers and the name: lines, words, and bytes, in that order. A word is any run of characters between spaces, tabs, or line breaks.

The Options

OptionCounts
-lLines
-wWords
-cBytes
-mCharacters
-LLength of the longest line

-c and -m differ for text with accented letters or other non-English characters: in a UTF-8 locale such a character takes two or more bytes but counts as one character with -m.

How Lines Are Counted

wc -l counts line break characters. A file whose last line does not end with a line break shows one line fewer than you see in an editor. For log files and most configuration files this makes no difference, because each line ends with a line break.

Several Files

wc -l /var/log/nginx/*.log

wc prints one line per file and a final total line. To count all lines of a project across folders, combine it with find:

find /var/www/app -name "*.php" -exec cat {} + | wc -l

More ways to select files are in the find command guide.

Count Command Output

  • Logged-in sessions: who | wc -l.
  • Running processes of a service: pgrep -c nginx counts directly, without wc.
  • Matching lines in one file: grep -c "error" app.log gives the same number as piping grep into wc -l.
  • Accounts with bash as their shell: grep -c "/bin/bash$" /etc/passwd.

Count Files in a Directory

find /var/www/uploads -maxdepth 1 -type f | wc -l
find /var/www/uploads -type f | wc -l
ls -A /var/www/uploads | wc -l

The first command counts files in the folder itself, the second also counts files in all subfolders. The third counts every entry, folders and hidden files included. A file name that contains a line break would be counted twice; such names are rare but possible. Many small files can fill the inode table of a disk; df -i shows this, as explained in how to check disk space in Linux.

Counting Unique Values

wc counts all lines, duplicates included. To count how many different IP addresses appear in a log, remove the duplicates first with sort -u:

awk '{print $1}' /var/log/nginx/access.log | sort -u | wc -l

The first part prints the first field of each line, which is the client address in the default Nginx format; see the awk command guide for how fields work.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations