![]() |
VOOZH | about |
The wc command in Linux is used to count lines, words, characters, and bytes in a file or from input you provide. Whether you're analyzing logs, reading large datasets, or just checking the size of your scripts, wc gives a quick summary. It can take one or more files as input or read directly from user input if no file is mentioned.
Let us consider two files having name state.txt and capital.txt containing 5 names of the Indian states and capitals respectively.
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
$ cat capital.txt
Hyderabad
Itanagar
Dispur
Patna
Raipur
Command:
$ wc state.txtOutput
5 7 58 state.txthere,
Similarly,
Command:
$ wc capital.txtOutput:
5 5 39 capital.txt$ wc state.txt capital.txt
5 7 58 state.txt
5 5 39 capital.txt
10 12 97 total
Note : When more than file name is specified in argument then command will display four-columnar output for all individual files plus one extra row displaying total number of lines, words and characters of all the files specified in argument, followed by keyword total.
Here are some commonly used options in wc command in linux:
wc [OPTION]... [FILE]...
If no file is specified, it will read from standard input, meaning you can type text manually or pipe it from another command.
Displays the number of lines in a file.
Command:
$ wc -l state.txt
Output:
5 state.txtCommand:
$ wc -l state.txt capital.txt
Output:
5 state.txt
5 capital.txt
10 total
This option prints the number of words present in a file. With this option wc command displays two-columnar output, 1st column shows number of words present in a file and 2nd is the file name.
Command:
$ wc -w state.txtOutput:
7 state.txtCommand:
$ wc -w state.txt capital.txt
Output:
7 state.txt
5 capital.txt
12 total
This option displays count of bytes present in a file. With this option it display two-columnar output, 1st column shows number of bytes present in a file and 2nd is the file name.
Command:
$ wc -c state.txt
Output:
58 state.txtCommand:
$ wc -c state.txt capital.txt
Output:
58 state.txt
39 capital.txt
97 total
Using -m option 'wc' command displays count of characters from a file.
Command:
$ wc -m state.txt
Output:
56 state.txt
Command:
$ wc -m state.txt capital.txt
Output:
58 state.txt
39 capital.txt
97 total
-L option in the wc command is used to display the length of the longest line (in terms of characters) in a file.state.txt, the line “Arunachal Pradesh” has the longest length (17 characters).capital.txt, the longest line is “Hyderabad” (10 characters).Command:
$ wc -L state.txt capital.txt
Output:
17 state.txt
10 capital.txt
17 total
Note: A character is the smallest unit of information that includes space, tab and newline.
This option is used to display the version of wc which is currently running on your system.
Command:
$ wc --versionOutput:
wc (GNU coreutils) 8.26
Packaged by Cygwin (8.26-1)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Paul Rubin and David MacKenzie.
Here are some applications of wc command
1. To count all files and folders present in directory:
As we all know ls command in unix is used to display all the files and folders present in the directory, when it is piped with wc command with -l option it display count of all files and folders present in current directory.
$ ls gfg
a.txt
b.txt
c.txt
d.txt
e.txt
geeksforgeeks
India
$ ls gfg | wc -l
7
2. Display number of word count only of a file:
We all know that this can be done with wc command having -w option, wc -w file_name, but this command shows two-columnar output one is count of words and other is file name.
$ wc -w state.txt
7 state.txt
So to display 1st column only, pipe(|) output of wc -w command to cut command with -c option. Or use input redirection(<).
$ wc -w state.txt | cut -c1
7
OR
$ wc -w < state.txt
7