The Linux wc command is a useful command that can come in handy if you ever want to know details like newline counts, byte counts, character counts etc for a file which is supplied as input. In this article, we will discuss 5 basic examples of this command.
List WC command options
WC command in unix or linux is used to find the number of lines, words and characters in a file. The syntax of wc command is shown below:
# wc [options] filenames
[su_note]You can use the following options with the wc command.
wc -l: print the newline counts
wc -w: print the word counts
wc -c: print the byte counts
wc -m: print the character counts
wc -L: print the length of the longest line
wc –help: display this help and exit
wc –version: output version information and exit[/su_note]
Let see how to use the wc command with few examples. Create the following file in your unix or linux operating system.
[rasho@Gandalf ~]# cat lintut.txt Red Hat CentOS Fedora Debian Scientific Linux
WC Command Examples
The ‘wc‘ command without passing any parameter will display a basic result of ”lintut.txt‘ file. The three numbers shown below are 5 (number of lines), 7 (number of words) and 46 (numberof bytes) of the file.
[rasho@Gandalf ~]# wc lintut.txt 5 7 46 lintut.txt
Printing count of lines
This is the most commonly used operation to find the number of lines from a file. Run the below command to display the number of lines:
[root@lintut ~]# wc -l lintut.txt 5 lintut.txt
Displaying the number of words.
Just use the -w option to find the count of words in a file. This is shown below:
[rasho@Gandalf ~]# wc -w lintut.txt 16 lintut.txt
Print count of bytes, count of characters from a file
We can use the -c and -m options to find the number of bytes and characters respectively in a file.
[rasho@Gandalf ~]# wc -c lintut.txt 46 lintut.txt
[rasho@Gandalf ~]# wc -m lintut.txt 46 lintut.txt
Print the length of longest line
The -L option is used to print the number of characters in the longest line from a file.
[root@lintut ~]# wc -L lintut.txt 16 lintut.txt
Show more wc options
For more information and help on the wc command, simple run the ‘wc –help‘ or ‘man wc‘ from the command line.
[rasho@Gandalf ~]# 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. A word is a non-zero-length sequence of characters delimited by white space. 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 length of the longest line -w, --words print the word counts --help display this help and exit --version output version information and exit Report wc bugs to bug-coreutils@gnu.org GNU coreutils home page: http://www.gnu.org/software/coreutils/> General help using GNU software: http://www.gnu.org/gethelp/> For complete documentation, run: info coreutils 'wc invocation'