wc Command: Counting Lines and Words, and Using wc -l with Pipes

Test environmentRHEL familyAlmaLinux 9.8root accountChecked 2026-10-29
Text viewing · editing commandswcLocation/usr/bin/wc

The wc command is a Linux command that counts and prints the number of lines, words and bytes in a file. Counting the lines of a sample web server access log with wc -l gives 10, and if you filter only the 404 responses with grep and pass them to wc -l, you can see the number of failed requests, 3, as a single number.

The logs, file contents and the working folder /root/work below are example values. The log is a ten-line file made with IP addresses reserved for documentation, and the other files were also all created for testing.

1️⃣ Basic usage of the wc command and reading its output

The basic form of the command is wc, then options, then files. If you give only a file without options, it prints the line count, word count and byte count in that order and appends the file name at the end.

cd /root/work
wc access.log
 10 100 766 access.log

In the result of counting the access log, 10 is the line count, 100 is the word count and 766 is the byte count.

To see just one number, add one of -l, -w or -c.

wc -l access.log
10 access.log
wc -w access.log
100 access.log
wc -c access.log
766 access.log

If you give a file that does not exist, an error message appears and the exit code is 1.

wc nosuch.txt
wc: nosuch.txt: No such file or directory
echo $?
1

2️⃣ Counting lines with wc -l and pipes

When the file name is printed along with the line count, you have to cut off the name once more when you store the number in a script variable. If you pass the file through redirection (<) or a pipe, wc does not know the file name, so it prints only the number.

wc -l < access.log
10
cat access.log | wc -l
10

Putting grep before the pipe lets you count only the lines that match a condition. The search included the spaces before and after the response code 404, and three lines contained 404.

grep ' 404 ' access.log | wc -l
3

To count how many IPs connected, cut out only the first field, remove duplicate lines with sort -u, and then count. In the ten-line log, there were four distinct IPs.

cut -d ' ' -f 1 access.log | sort -u
192.0.2.10
192.0.2.44
198.51.100.7
203.0.113.25
cut -d ' ' -f 1 access.log | sort -u | wc -l
4

wc -l counts the number of newline characters. So for a file with no newline at the end of the last line, wc -l prints 1 even though cat shows two lines.

cat no-newline.txt
first line
last line
wc -l no-newline.txt
1 no-newline.txt

If the line count comes out one short, first check whether the last line ends with a newline.

3️⃣ wc options and character counts with -c and -m

In UTF-8, English letters and digits take 1 byte per character, while Korean (Hangul) characters and ☕ take 3 bytes each. So in a file with only English text, the byte count from -c and the character count from -m are the same, but once Korean is included, the two numbers differ. The comparison uses a file with two lines of Korean menu names and one line in English.

cat menu.txt
아메리카노
카페 라테 ☕
tea
wc -c menu.txt
38 menu.txt
wc -m menu.txt
18 menu.txt

-c gave 38 and -m gave 18, a difference of 20. That is the extra 2 bytes taken by each of the nine Korean characters and ☕, so for a file containing Korean, count characters with -m to match the actual number of characters. The measurement was done in the C.UTF-8 locale, and according to the manual, -m counts in the character units of the locale.

-L prints the display width of the longest line. In the access log, the longest line was 82 columns wide; in the menu file, each Korean character and ☕ takes two columns, so a 7-character line came out as 12.

wc -L access.log
82 access.log
wc -L menu.txt
12 menu.txt

Accented Latin letters take 2 bytes per character in UTF-8. This was measured separately with a file containing only the single line café.

cat cafe.txt
café
wc -c cafe.txt
6 cafe.txt
wc -m cafe.txt
5 cafe.txt

-c gave 6 and -m gave 5, because the single é took one extra byte. English letters and digits take 1 byte, accented letters such as é take 2 bytes, and Korean characters and ☕ take 3 bytes, so the byte count differs even for the same number of characters.

The table below matches each task with a command and the number counted in this article.

TaskCommandMeasured in this article
Get only the line count as a numberwc -l < access.log10Number only, without the file name
Count lines that match a conditiongrep ‘ 404 ‘ access.log | wc -l3Lines with a 404 response
Count distinct valuescut -d ‘ ‘ -f 1 access.log | sort -u | wc -l4Number of distinct IPs
Check the file sizewc -c access.log766Byte count
Character count of a file containing Koreanwc -m menu.txt1838 when the same file is counted with -c
Display width of the longest linewc -L access.log8212 for the menu file, which counts each Korean character as two columns
Total line count of several fileswc -l *.log13Total on the total line

4️⃣ Totals for multiple files with Linux wc

If you give several files, wc prints one line per file and adds the total on the last line as total. When you count the access log and the error log together, the 13 on the total line is the sum of the line counts of the two files.

wc access.log error.log
 10 100 766 access.log
  3  11  76 error.log
 13 111 842 total

To count logs with the same extension all at once, use a wildcard. The shell expands *.log into file names before passing them on, so the result is the same as listing each file name.

wc -l *.log
 10 access.log
  3 error.log
 13 total

Files with different extensions can also be counted at once by listing their names one after another. Counting the two logs and the menu file together gave total 16.

wc -l access.log error.log menu.txt
 10 access.log
  3 error.log
  3 menu.txt
 16 total

🔲 wc command simulator

In the simulator below, you can type wc yourself on the sample files from this article. The chips let you try -l, -m and pipe counting, and if you add a line to access.log and count again, the number goes up.

wc command simulatorwc / root account
[root@server work]#
Use the ↑↓ keys to recall commands you typed earlier.

🔢 FAQ & Recommended Content

Yes, when you count matching lines in a single file. On the same access log, grep -c with the same condition also gave 3. grep -c finishes in a single command without a pipe, while wc -l is used to count the output of other commands besides grep, such as sort or cut.

It counts each chunk separated by spaces, tabs or newlines as one word. The result of [echo ‘log_level=warn timeout = 30’ | wc -w] is 4. log_level=warn, with no spaces in it, counts as one word, and the = with spaces on both sides counts as a separate word.

Select only files with find and pass them to wc -l. In the sample folder, [find /root/work -type f | wc -l] printed 5. [ls | wc -l] also gave 5 in the same folder, but ls also outputs subfolder names as lines and does not count the files inside them.

ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 10   +   1   =