Unix/Linux wc command examples

In this article, let’s see wc Unix command. This Unix command is used to print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.

wc syntax

Below is syntax wc Unix command

wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F

Options

These are optional additional parameters you pass to this command.

-c:- print the byte counts
-m:- print the character counts
-l:- print the newline counts
-L:- print the length of the longest line
-W:- print the word counts
–help:- Print help
–version:- Display version information

FILE

Input file name

Examples

no option

$ wc wc_demo.txt
1 4 20 wc_demo.txt

-c — bytes

print the byte counts

$ wc -c wc_demo.txt
20 wc_demo.txt

-m  –chars

print the character counts

$ wc -m wc_demo.txt
20 wc_demo.txt

-l –lines

print the newline counts

$ wc -l wc_demo.txt
1 wc_demo.txt

 

Note: wc works on /n lines character. If there is no line character at the end of file, count will be one less. Check this thread on StackOverflow The wc -l gives wrong result

-L, –max-line length

print the length of the longest line

wc -L wc_demo.txt
19 wc_demo.txt

-w, –words

print the word counts

$ wc -w wc_demo.txt
4 wc_demo.txt

–help

display this help and exit

–version

output version information and exit

Related Post

Unix Commands

whoami is a Unix command to print the username associated with the current effective user ID.…