wc Command in Linux

The wc command (short for word count) counts the number of lines, words, and characters in a file or list of files in Linux and Unix-like systems. It can read from either standard input or multiple files and provides the following statistics:

  • Line Count: Counting the number of lines in the input.
  • Word Count: The number of words in the input. A word is defined as a sequence of characters separated by spaces, tabs, or newline characters.
  • Byte Count: The number of bytes in the input.

If you specify a list of files, wc command will provide statistics for each file individually, followed by a total count for all named files. If no file is specified, it uses standard input.

Syntax:

The basic syntax of the wc command is as follows:

wc [OPTION]... File...

The File argument is the name of the file or set of files that you want to count. The OPTION argument is used to specify which type of count you want to perform.

wc Options:

advertisement
advertisement
  • -c: Count the number of bytes.
  • -m or -C: Count the number of characters.
  • -l: Count the number of lines.
  • -w: Count the number of words separated by white space characters or new line characters.
  • file: The name of the file to be counted. If no file is specified, standard input is used.

wc Command Examples:

Example 1: Counting Lines (-l Option)

To count the number of lines in a file, use the -l option. For example, to count the lines in a file named “myfile.txt,” run:

wc -l myfile.txt

If myfile.txt contains the following text:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
This is the first line.
This is the second line.
This is the third line.

The output will be:

3

This means that “myfile.txt” contains 3 lines.

advertisement

Example 2: Counting Words (-w Option)

To count the words in a file, use the -w option. For example, the following command will count the number of words in “myfile.txt”:

wc -w myfile.txt

If “myfile.txt” contains the same text as above, the output will be:

7

This means “myfile.txt” contains 7 words.

advertisement

Example 3: Counting Characters (-m Option)

To count the characters in a file, use the -m option. For example, to count characters in “myfile.txt,” run:

wc -m myfile.txt

If “myfile.txt” contains the same text as above, the output will be:

27

This means that “myfile.txt” contains 27 characters.

Example 4: Counting Bytes (-c Option)

The -c option counts the number of bytes in a file, similar to the -m option but measuring bytes. To count bytes in “myfile.txt,” run:

wc -c myfile.txt

If “myfile.txt” contains the same text as above, the output will be:

27

This means that “myfile.txt” contains 27 bytes.

Example 5: Counting Lines, Words, and Characters in a File

To display Line, word and character count in “myfile.txt” use the following command.

wc myfile.txt

This command will provide the line count, word count, and character count for the “myfile.txt” file, allowing you to analyze its content.

Example 6: Counting the Length of the Longest Line

The -L option counts the length of the longest line in a file. For example, the following command will count the length of the longest line in the file myfile.txt:

wc -L myfile.txt

If “myfile.txt” contains the same text as above, the output will be:

20

This means that the longest line in “myfile.txt” is 20 characters long.

Example 7: Counting Multiple Files

The wc command can also be used to count multiple files at once. To do this, simply specify the names of the files as arguments to the command. For example, the following command will count the number of lines in the files myfile1.txt and myfile2.txt:

wc -l myfile1.txt myfile2.txt

The output of the command will be the total number of lines in the two files.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.