Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
LPI Linux Essentials Exam 010-160 - Topic 3.2 - wc Command
The wc
command in Linux, which stands for "word count," is a highly versatile tool that allows you to count the number of lines, words, and characters in text files or data streams. This guide aims to offer a comprehensive walkthrough, from basic usage to advanced options, for anyone who wishes to harness the capabilities of this handy command. Whether you're a system administrator looking to gain insights into log files, a developer interested in quickly summarizing code statistics, or simply a curious user wanting to explore text data, this guide will provide you with the tools you need.
Usage
The wc
command is commonly executed with the following syntax:
wc [options] [file...]
In this syntax, options
are the flags that alter the command's behavior, and file
specifies the file or files you wish to examine. If you don't specify a file, the command will read from the standard input.
Counting Lines
The -l
option allows you to easily count the number of lines in a file.
Exercise: Counting lines:
wc -l /etc/passwd
The output will resemble:
54 /etc/passwd
Counting Words
If you're more interested in counting the number of words, use the -w
option.
Exercise: Counting words:
wc -w /etc/passwd
The output will resemble:
94 /etc/passwd
Counting Characters
To discover the total number of characters in your file, use the -c
option.
Exercise: Counting characters:
wc -c /etc/passwd
The output will resemble:
3197 /etc/passwd
Counting Bytes
You might be interested in the byte size of a file rather than the character count. In that case, use the -m
option. Note that the majority of the time the byte and character counts are usually the same.
Exercise: Counting bytes:
wc -m /etc/passwd
The output will resemble:
3197 /etc/passwd
Default Counts
If you want a comprehensive view that includes lines, words & characters, just use no options at all.
Exercise: Counting lines, words & characters:
wc /etc/passwd
The output will resemble:
54 94 3197 /etc/passwd 10 /etc/hosts
Counting Multiple Files
There may be a scenario that requires the counting of multiple files, and the wc
command can do this in one go, providing a combined total count.
Exercise: Line count multiple files:
wc -l /etc/passwd /etc/hosts
The output will resemble:
54 /etc/passwd
10 /etc/hosts
64 total
Wrapping Up
Mastering the wc
command in Linux opens up a range of possibilities for text analysis and data extraction. From basic word and line counting to more complex analyses with multiple metrics, this tool is an essential part of any Linux user's toolkit.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.