wc Command: Counting Lines and Words, and Using wc -l with Pipes
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/workwc access.log 10 100 766 access.logIn 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.log10 access.logwc -w access.log100 access.logwc -c access.log766 access.logIf you give a file that does not exist, an error message appears and the exit code is 1.
wc nosuch.txtwc: nosuch.txt: No such file or directoryecho $?12️⃣ 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.log10cat access.log | wc -l10Putting 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 -l3To 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 -u192.0.2.10
192.0.2.44
198.51.100.7
203.0.113.25cut -d ' ' -f 1 access.log | sort -u | wc -l4wc -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.txtfirst line
last linewc -l no-newline.txt1 no-newline.txtIf 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아메리카노
카페 라테 ☕
teawc -c menu.txt38 menu.txtwc -m menu.txt18 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.log82 access.logwc -L menu.txt12 menu.txtAccented 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.txtcaféwc -c cafe.txt6 cafe.txtwc -m cafe.txt5 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.
| Task | Command | Measured in this article |
|---|---|---|
| Get only the line count as a number | wc -l < access.log | 10Number only, without the file name |
| Count lines that match a condition | grep ‘ 404 ‘ access.log | wc -l | 3Lines with a 404 response |
| Count distinct values | cut -d ‘ ‘ -f 1 access.log | sort -u | wc -l | 4Number of distinct IPs |
| Check the file size | wc -c access.log | 766Byte count |
| Character count of a file containing Korean | wc -m menu.txt | 1838 when the same file is counted with -c |
| Display width of the longest line | wc -L access.log | 8212 for the menu file, which counts each Korean character as two columns |
| Total line count of several files | wc -l *.log | 13Total 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 totalTo 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 totalFiles 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.
🔢 FAQ & Recommended Content
Linux · Text
Linux sort command: sorting files
sort options and examples for sorting file contents alphabetically or in ascending or descending order.
Linux · Text
Linux uniq command
How to use uniq to keep only one of each duplicate line and count repetitions with -c.
Linux · Text
Linux head command & tail command
Options and usage of head, which prints the first 10 lines of a file, and tail, which prints the last 10 lines.
ℹ️ 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.