WC stands for word count. As the name implies, it is mainly used for counting purposes.
- Used to find out the number of lines, word count, byte count, and character count in files specified in the arguments of the file
- By default, it displays four-column output.
- third column shows the number of characters present in the file, and the fourth column itself is the name of the file that is given as an argument.
.
The first column shows the number of lines present in a specified file, the second column shows the number of words present in the file, the
Syntax:
wc [OPTION]… [ARCHIVE]…
Consider two files that have name state.txt and capital.txt containing 5 names of the Indian states and capitals respectively.
$ cat state.txt$ cat capital.txt Pass only one file name in the argument. $ wc state.txt 5 7 58 state.txt O $ wc capital.txt 5 5 39 capital.txt
Pass more than one file name in the argument.
$ wc
state.txt capital.txt 5 7 58 state.txt 5 5 39
capital.txt
10 12 97 total
Note: When more than the file name is specified in the argument, the command displays four-column output for all individual files plus an additional row showing the total number of lines, words, and characters for all files specified in the argument, followed by the total keyword. Options: 1. -l: This option prints the number of lines present in a file. With this option, the wc command displays the output of two columns, the 1st column displays the number of lines present in a file, and the 2nd represents the file name.
With a file name $ wc -l state.txt 5 state.txt With more than one file name $ wc -l state.txt capital.txt 5 state.txt 5 capital.txt 10 total 2.
-w: This option prints the number of words present in a file. With this option, the wc command displays the output of two columns, the 1st column shows the number of words present in a file, and the 2nd is the name of the file.
With a file name $ wc -w state.txt 7 state.txt With more than one file name $ wc -w state.txt capital.txt 7 state.txt 5 capital.txt 12 total 3.
-c: This option displays the byte count present in a file. With this option it shows the output of two columns, the 1st column shows the number of bytes present in a file and the 2nd is the name of the file.
With a filename $ wc -c state.txt 58 state.txt With more than one filename $ wc -c state.txt capital.txt 58 state.txt 39 capital.txt 97 total 4. -m: Using the ‘wc
‘ command in the -m option displays the character count of a file. With a file name $ wc -m state.txt 56 state.txt
With more than one file name $ wc -m state.txt capital.txt 58 state.txt 39 capital.txt 97 total 5. -L: The ‘wc‘ command allows a -L argument, it can be used to print the length of the longest line (number of characters) in a
file. Thus, we have the longest character line Arunachal Pradesh in an archive state.txt and Hyderabad in the archive capital.txt. But with this option, if more than one file name is specified, the last row, that is, the additional row, does not display the total, but displays the maximum of all values displayed in the first column of individual files. note: A character is the smallest unit of information that includes space, tab, and new line.
With a file name $ wc -L state.txt 17 state.txt With more than one file name $ wc -L state.txt capital.txt 17 state.txt 10 capital.txt 17 total
6. -version: This option is used to display the version of wc that is currently running on your system.
$ wc -version wc (GNU coreutils) 8.26 Packaged by Cygwin (8.26-1) Copyright (C) 2016 Free Software Foundation, Inc. GPLv3+ license: GNU GPL version 3 or later. This is free software: you are free to change and redistribute it. NO WARRANTY, to the extent permitted by law. Writing by Paul Rubin and David MacKenzie.
WC Applications Command
1. To count all files and folders present in the directory: As we all know, the ls command in unix is used to display all the files and folders present in the directory, when piped with the wc command with the -l option, it shows the count of all the files and folders present in the current directory.
$ ls gfg a.txt b.txt c.txt d.txt e.txt geeksforgeeks India $ ls gfg | wc -l 7
2. Show the number of words only of a file: We all know that this can be done with the wc command that has the option -w, wc -w file_name, but this command shows the output of two columns, one is the word count and the other is the name of the file.
$ wc -w state.txt 7 state.txt Therefore, to
display only the 1st column, the pipe(|) output of the wc -w command to cut the command with the -c option. O use input redirection(<).
$ wc -w state.txt | cut -c1 7 O $ wc -w < state.txt 7
?t=89 This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See their article listed on the GeeksforGeeks homepage and help other Geeks. Please write comments if you find something wrong, or if you want to share more information about the topic discussed above.