wc command in Linux with Examples



Name

wc - print newline count, word count and byte count for each file.

Synopsis

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

Options

The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length.

-c, --bytes
   print the byte counts

-m, --chars
   print the character counts

-l, --lines
   print the newline counts

--files0-from=F
   read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input

-L, --max-line-length
   print the maximum display width
    
-w, --words
   print the word counts

--help
   display this help and exit

--version
   output version information and exit

Description

wc stands for word count is a command in Unix and Unix-like operating systems. It is mainly used for counting purpose.

By default it displays four-columnar output. First column shows number of lines present in a file specified, second column shows number of words present in the file, third column shows number of characters present in file and fourth column itself is the file name which are given as argument.

‘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.

Examples

Let us consider two files having name test1.txt and test2.txt

$ cat test1.txt
delhi
mumbai
bangalore
chennai
kolkata
$ cat test2.txt 
a) a.k. shukla
b) anat hari
c) barun kumar
d) jai sharma
e) sumit singh  

1. Passing only one file name in the argument and count number of lines, words and characters.

$ wc test1.txt
5  5 39 test1.txt
$ wc test2.txt
5 15 72 test2.txt

2. wc command allow us to pass more than one file name in the argument.

$ wc test1.txt test2.txt 
5     5    39 test1.txt
5    15    72 test2.txt
10   20   111 total
$wc Bikaner_*
229   2513  45154 Bikaner_eng2ban_gmt.txt
229   2756  46626 Bikaner_hin2ban_bmt.txt
229   2802  48698 Bikaner_hin2ban_gmt.txt
687   8071 140478 total

3. wc command allow us to count lines, words and characters in utf-8 encoded files as well.pass more than one file name in the argument.

$ wc test1.txt test2.txt 
5     5    39 test1.txt
5    15    72 test2.txt
10   20   111 total
$wc Bikaner_*
229   2513  45154 Bikaner_eng2ban_gmt.txt
229   2756  46626 Bikaner_hin2ban_bmt.txt
229   2802  48698 Bikaner_hin2ban_gmt.txt
687   8071 140478 total

4. To print number of lines present in a file use -l or --lines option.

$ wc -l test2.txt 
5 test2.txt
$ wc --lines test2.txt 
5 test2.txt

5. To print the numbers of bytes in a file use -c or --bytes option.

$ wc -c test1.txt 
39 test1.txt
$ wc --bytes test1.txt 
39 test1.txt

6. To print the number of characters in a file use -m or --chars option.

$ wc -m test2.txt 
72 test2.txt
$ wc --chars test2.txt 
72 test2.txt

7. To print the number of words present in a file use -w or --words option.

$ wc -w test1.txt
5 test1.txt
$ wc --words test1.txt 
5 test1.txt

8. In case of files that contain characters that have multiple bytes (UTF-8), the number of bytes reported is different than the number of characters. These files that are listed below contain Hindi and Bangla Unicode characters with UTF-8 encodings. So we can see that byte count reported by '-b' option is different than characters count reported by '-m' option.

$ wc -c Bikaner_*
45154 Bikaner_eng2ban_gmt.txt
46626 Bikaner_hin2ban_bmt.txt
48698 Bikaner_hin2ban_gmt.txt
140478 total

$ wc -m Bikaner_*
17394 Bikaner_eng2ban_gmt.txt
17952 Bikaner_hin2ban_bmt.txt
18702 Bikaner_hin2ban_gmt.txt
54048 total

9. The wc command can be used in combination with other commands through piping.

We can Count the number of files in the Current Directory by the help of wc command. The find command passes a list of all files in the current directory with each file name on a single line to the wc command, which counts the number of lines.

$ find . -type f | wc -l
64425

10. To count the number of records (or rows) in several CSV files the wc can used in conjunction with pipes.

$ cat *.csv | wc -l 
4301
Advertisements