‘wc’: Print newline, word, and byte counts
‘wc’ counts the number of bytes, characters, whitespace-separated words, and newlines in each given FILE, or standard input if none are given or for a FILE of ‘-‘.
wc [OPTION]… [FILE]…
‘wc’ prints one line of counts for each file, and if the file was given as an argument, it prints the file name following the counts. If more than one FILE is given, ‘wc’ prints a final line containing the cumulative counts, with the file name ‘total’. The counts are printed in this order: newlines, words, characters, bytes, maximum line length.
Each count is printed right-justified in a field with at least one space between fields so that the numbers and file names normally line up nicely in columns. The width of the count fields varies depending on the inputs, so you should not depend on a particular field width. However, as a GNU extension, if only one count is printed, it is
guaranteed to be printed without leading spaces.
By default, ‘wc’ prints three counts: the newline, words, and byte counts. Options can specify that only certain counts be printed. Options do not undo others previously given, so
wc –bytes –words
prints both the byte counts and the word counts.
With the ‘–max-line-length’ option, ‘wc’ prints the length of the longest line per file, and if there is more than one file it prints the maximum (not the sum) of those lengths. The line lengths here are measured in screen columns, according to the current locale and assuming tab positions in every 8th column.
The program accepts the following options.
‘-c’ | ‘–bytes’
Print only the byte counts.
‘-m’ | ‘–chars’
Print only the character counts.
‘-w’ | ‘–words’
Print only the word counts.
‘-l’ | ‘–lines’
Print only the newline counts.
‘-L’ | ‘–max-line-length’
Print only the maximum line lengths.
Disallow processing files named on the command line, and instead process those named in file FILE; each name being terminated by a zero byte (ASCII NUL).
This is useful when the list of file names is so long that it may exceed a command line length limitation. In such cases, running ‘wc’ via ‘xargs’ is undesirable because it splits the list into pieces and makes ‘wc’ print a total for each sublist rather than for the entire list.
One way to produce a list of ASCII NUL terminated file names is with GNU ‘find’, using its ‘-print0’ predicate. If FILE is ‘-‘ then the ASCII NUL terminated file names are read from standard input.
For example, to find the length of the longest line in any ‘.c’ or ‘.h’ file in the current hierarchy, do this:
find . -name ‘*.[ch]’ -print0 | wc -L –files0-from=- | tail -n1
An exit status of zero indicates success, and a nonzero value indicates failure.
Examples of WC command
[root@igurkul ~]# cat igurkul.txt
1. A Basic Example of WC Command
The ‘wc‘ command without passing any parameter will display a basic result of ”igurkul.txt‘ file. The three numbers shown below are 12 (number of lines), 16 (number of words) and 112 (number of bytes) of the file.
[root@igurkul ~]# wc igurkul.txt
12 16 112 igurkul.txt
2. Count Number of Lines
To count number of newlines in a file use the option ‘-l‘, which prints the number of lines from a given file. Say, the following command will display the count of newlines in a file. In the output the first filed assigned as count and second field is the name of file.
[root@igurkul ~]# wc -l igurkul.txt
3. Display Number of Words
Using ‘-w‘ argument with ‘wc‘ command prints the number of words in a file. Type the following command to count the words in a file.
[root@igurkul ~]# wc -w igurkul.txt
4. Count Number of Bytes and Characters
When using options ‘-c‘ and ‘-m‘ with ‘wc‘ command will print the total number of bytes and characters respectively in a file.
[root@igurkul ~]# wc -c igurkul.txt
[root@igurkul ~]# wc -m igurkul.txt
5. Display Length of Longest Line
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.
[root@igurkul ~]# wc -L igurkul.txt
6. Check More WC Options
For more information and help on the wc command, simple run the ‘wc –help‘ or ‘man wc‘ from the command line.
[root@igurkul ~]# wc –help
Usage: wc [OPTION]… [FILE]…
or: wc [OPTION]… –files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. With no FILE, or when FILE is -,
read standard input.
-c, –bytes print the byte counts
-m, –chars print the character counts
-l, –lines print the newline counts
-L, –max-line-length print the length of the longest line
-w, –words print the word counts
–help display this help and exit
–version output version information and exit