wc Command in Linux

«
»

This tutorial explains Linux “wc” command, options and its usage with examples.

wc – Short for word count, wc displays a count of lines, words, and characters in a file.

DESCRIPTION

The program reads either standard input or a list of files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow. If a file is not specified for the File parameter, standard input is used. The command writes the results to standard output and keeps a total count for all named files. If flags are specified, the ordering of the flags determines the ordering of the output. A word is defined as a string of characters delimited by spaces, tabs, or newline characters.

SYNOPSIS

wc [-c | -m | -C ] [-l] [-w] [ file … ]

OPTIONS :

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

-c
Count bytes.
-m
Count characters.
-C
Same as -m.
-l
Count lines.
-w
Count words delimited by white space characters or new line characters.
file
Name of file to word count.

EXAMPLES

1. Displaying Line,word and character count in myfile.txt.

$ wc myfile.txt
5    13    57   myfile.txt

5 = Lines
13 = Words
57 = Characters

2. Count how many files and directories are in the current directory

$ ls -1 | wc -l

This command uses the ls command to list files in a bare format and pipes the output into the wc command to count how many files are listed. When done properly, the terminal should return a single number indicating how many lines were counted and then return you to the prompt.

advertisement

3. Display Length of Longest Line

$ wc -L abc

The ‘wc‘ command allow an argument ‘-L‘, it can be used to print out the length of longest (number of characters) line in a file. So, we have the longest character line (‘Scientific Linux‘) in a file.

advertisement

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 & technical discussions at Telegram SanfoundryClasses.