6 WC Command Examples to Count Number of Lines, Words, Characters in Linux

[‘

n

The wc (word count) command in Unix/Linux operating systems is used to find out number of newline count, word count, byte and characters count in a files specified by the file arguments. The syntax of wc command as shown below.

n

# wc [options] filenames

n

The following are the options and usage provided by the command.

n

wc -l : Prints the number of lines in a file.rnwc -w : prints the number of words in a file.rnwc -c : Displays the count of bytes in a file.rnwc -m : prints the count of characters from a file.rnwc -L : prints only the length of the longest line in a file.

n

So, let’s see how we can use the ‘wc‘ command with their few available arguments and examples in this article. We have used the ‘tecmint.txt‘ file for testing the commands. Let’s find out the output of the file using cat command as shown below.

n

[[emailxa0protected] ~]# cat tecmint.txtrnrnRed HatrnCentOSrnFedorarnDebianrnScientific LinuxrnOpenSusernUbunturnXubunturnLinux MintrnPearl LinuxrnSlackwarernMandriva

n

1. A Basic Example of WC Command

n

The ‘wc‘ command without passing any parameter will display a basic result of ”tecmint.txt‘ file. The three numbers shown below are 12 (number of lines), 16 (number of words) and 112 (number of bytes) of the file.

n

[[emailxa0protected] ~]# wc tecmint.txtrnrn12  16 112 tecmint.txt

n

2. Count Number of Lines

n

To count number of newlines in a file use the option ‘-l‘, which prints the number of lines from a given file. Say, the following command will display the count of newlines in a file. In the output the first filed assigned as count and second field is the name of file.

n

[[emailxa0protected] ~]# wc -l tecmint.txtrnrn12 tecmint.txt

n

3. Display Number of Words

n

Using ‘-w‘ argument with ‘wc‘ command prints the number of words in a file. Type the following command to count the words in a file.

n

[[emailxa0protected] ~]# wc -w tecmint.txtrnrn16 tecmint.txt

n

4. Count Number of Bytes and Characters

n

When using options ‘-c‘ and ‘-m‘ with ‘wc‘ command will print the total number of bytes and characters respectively in a file.

n

[[emailxa0protected] ~]# wc -c tecmint.txtrnrn112 tecmint.txt

n

[[emailxa0protected] ~]# wc -m tecmint.txtrnrn112 tecmint.txt

n

5. Display Length of Longest Line

n

The ‘wc‘ command allow an argument ‘-L‘, it can be used to print out the length of longest (number of characters) line in a file. So, we have the longest character line (‘Scientific Linux‘) in a file.

n

[[emailxa0protected] ~]# wc -L tecmint.txtrnrn16 tecmint.txt

n

6. Check More WC Options

n

For more information and help on the wc command, simple run the ‘wc –help‘ or ‘man wc‘ from the command line.

n

[[emailxa0protected] ~]# wc --helprnrnUsage: wc [OPTION]... [FILE]...rn  or:  wc [OPTION]... --files0-from=FrnPrint newline, word, and byte counts for each FILE, and a total line ifrnmore than one FILE is specified.  With no FILE, or when FILE is -,rnread standard input.rn  -c, --bytes            print the byte countsrn  -m, --chars            print the character countsrn  -l, --lines            print the newline countsrn  -L, --max-line-length  print the length of the longest linern  -w, --words            print the word countsrn      --helptttdisplay this help and exitrn      --versiontttoutput version information and exitrnrnReport wc bugs to [emailxa0protected]rnGNU coreutils home page: <http://www.gnu.org/software/coreutils/>rnGeneral help using GNU software: <http://www.gnu.org/gethelp/>rnFor complete documentation, run: info coreutils 'wc invocation'

n

‘]