Skip to main content

Module 3: Text Processing & Search

Lesson 33: wc Command

In this lesson, you'll learn how to use the wc command to count lines, words, characters, and bytes in files in Linux.

wc (short for word count) is a command-line tool in Unix/Linux operating systems, which is used to find out the number of newline count, word count, byte and character count in the files specified by the File arguments to the standard output and hold a total count for all named files.

When you define the File parameter, the wc command prints the file names as well as the requested counts.

If you do not define a file name for the File parameter, it prints only the total count to the standard output.

Let's explore how to use the wc command to calculate a file's newlines, words, characters, or byte count with practical examples.

wc Command Syntax

The syntax of the wc command is shown below.

# wc [OPTIONS] FILENAMES

wc Command Options

Option Description
-l Print the number of lines in a file
-w Print the number of words in a file
-c Display the count of bytes in a file
-m Print the count of characters from a file
-L Print only the length of the longest line in a file

Let's see how we can use the wc command with the few available arguments and examples. We have used the tecmint.txt file for testing the commands.

Let's find out the output of the tecmint.txt file using the cat command as shown below.

$ cat tecmint.txt
Red Hat
CentOS
AlmaLinux
Rocky Linux
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Deepin Linux
Slackware
Mandriva

1. Basic wc Command Usage

The wc command without passing any parameter will display a basic result of the tecmint.txt file.

The three numbers shown below are 14 (number of lines), 19 (number of words), and 135 (number of bytes) of the file.

$ wc tecmint.txt
 14  19 135 tecmint.txt

2. Count Number of Lines in a File

Count the number of newlines in a file using the option -l, which prints the number of lines from a given file. The following command will display the count of newlines in a file.

In the output, the first field is assigned as count and the second field is the name of the file.

$ wc -l tecmint.txt
14 tecmint.txt

3. Count Number of Words in a File

Pro TecMint Β· Root Plan
This Article is for Root Members
Join Root to read the full article and unlock everything

Full Access to Every Article, Course & Certification Track

Join thousands of Linux professionals who use Pro TecMint to advance their careers.

Ad-free access to all premium articles
All courses: Learn Linux, Bash, Golang, Ubuntu and more
RHCSA, RHCE, LFCS & LFCA certification prep
New courses added every month
Private Telegram community & priority support
Root Plan
$8/mo
or $59/year billed annually
Save $37 with annual plan
Start Reading This Article in the Next 60 Seconds
Join Root Plan β†’