Syntax error Linux WC Command Examples to Count Number of Lines, Words, Characters

Linux WC Command Examples to Count Number of Lines, Words, Characters



The wc command also known as word count command is a fundamental command any Linux user should be aware of. It is mostly used with the l switch to do a line count, but it can actually give count of multiple things with various arguments supplied to it. Below is a list of what are those possible arguments. The we will see examples on each of them.

Sr.No Command Usage
1 wc -c Display number of bytes
2 wc -m Display number of characters
3 wc -w Display number of words
4 wc -l Display number of lines
5 wc -L Display length of longest line

Lets consider the below file for our examples.

ubuntu@ubuntu:~$ cat inspire.txt
Mastering anything needs practice.
It aslo needs patience.
And it needs time and other resources.

-c Option

Display the number of bytes in the file.

ubuntu@ubuntu:~$ wc -c inspire.txt

Running the above code gives us the following result:

98 inspire.txt

-m Option

Display the number of characters in the file.

ubuntu@ubuntu:~$ wc -m inspire.txt

Running the above code gives us the following result −

98 inspire.txt

-w Option

Display the number of words in the file.

ubuntu@ubuntu:~$ wc -w inspire.txt

Running the above code gives us the following result −

15 inspire.txt

-l Option

Display the number of lines in the file.

ubuntu@ubuntu:~$ wc -l inspire.txt

Running the above code gives us the following result:

3 inspire.txt

-L Option

Display the length of longest line in the file.

ubuntu@ubuntu:~$ wc -L inspire.txt

Running the above code gives us the following result −

38 inspire.txt

Only wc

Display the number of lines, words, and characters one after the other.

ubuntu@ubuntu:~$ wc inspire.txt

Running the above code gives us the following result −

3 15 98 inspire.txt
Updated on: 2020-01-31T12:19:34+05:30

477 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements