wc Command: Tutorial & Examples
Count the number of lines, words, and characters in a file
The wc
command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a given file or input. The basic syntax for the command
is wc [options] [file(s)]
.
The wc
command can be used with several options:
-l
: counts the number of lines in a file-w
: counts the number of words in a file-c
: counts the number of characters in a file-m
: counts the number of bytes in a file
For example, to count the number of lines in a file named example.txt
, you would use the command wc -l example.txt
. This would return the number of lines in the file, followed
by the name of the file.
You can also use wc
to count the number of lines, words and characters in multiple files by providing more than one file name as an argument. And you can also use it with
pipelines, for example if you want to count the number of lines in the output of a command, you can use command | wc -l
.
It is important to note that the wc
command does not change the original file, it only provides a count of the lines, words, and characters.
Further Reading
- Bash Cookbook by Carl Albing, J.P. Vossen
- Wicked Cool Shell Scripts by Dave Taylor, Brandon Perry
- Black Hat Bash by Nick Aleks, Dolev Farhi
- Bash Pocket Reference by Arnold Robbins
- The Linux Command Line by William Shotts
- Learning the Bash Shell by Cameron Newham
- Mastering Linux Shell Scripting by Mokhtar Ebrahim, Andrew Mallett
- Linux Command Line and Shell Scripting Bible by Richard Blum, Christine Bresnahan
- Shell Scripting by Jason Cannon