In the next article we are going to take a look at how we can use the WC command in Ubuntu. WC stands for ‘Word Count’, and it is a command used on Unix systems that allows you to perform different counts from the standard input, whether of words or characters, among other things.
The program lets you read standard input or a concatenated list, and generates one or more of the following statistics: line count, word count, and byte count. This command can accept one or more input file names.
WC command examples
The required syntax To use this command it would be something like the following:
wc [ OPCIONES ] ... [ARCHIVO] ...
Now, let’s see some simple examples. To work with these I am going to use two files. The first is called versions.txt and the other names.txt. The content of these is as follows:
Basic toilet use
If we pass just a filename in the argument we will get the count of lines, words and bytes. To see this result on the previous files, in a terminal (Ctrl + Alt + T) we only need to use the commands:
Plus also we can pass more than one filename in command argument:
wc versiones.txt nombres.txt
As you can see from the above screenshot, when more than the filename is specified in the argument, the command will display four column output for all individual files. A larger row is included in which the total number of lines, words and characters of all files is shown specified in the argument.
WC command options
In addition to using this command as we have seen so far, WC is a simple command to work with and comes with only a handful of options that can be interesting to use at times:
- -l, –lines : print the number of lines present in the file.
- -w, –words: print the total number of words in the File.
- -m, –chars: print the number of characters from the file.
- -L, –max-line-Length: prints the size of the longest line from the file.
- -c, –bytes: prints the total number of bytes in the File.
Option -l, –lines
This option will print the total number of lines in the file. The information is printed in two columns. The first column shows the number of lines present, and the second column shows the name of the past file.
wc -l nombres.txt
Option -w, –words
The -wo –words option shows the total number of words present in a file. Print the results in two columns. The first column shows the total number of words, and the second column shows the file name.
wc -w nombres.txt
Option -m, –chars
The -m or –chars option displays the total number of characters in the file. Print the results in two columns. The first column shows the total number of characters in the file, and the second column shows the name of the file.
wc -m nombres.txt
Option -L, –max-line-length
The -L (capital letters) print the length (number of characters) of the longest line in the file.
wc -L nombres.txt
Option -c, –bytes
This option displays the count of bytes present in the file. Print the results in two columns. The first column shows the total number of bytes in the file, and the second column shows the name of the passed file.
wc -c nombres.txt
Combine WC with other commands
In addition to the options we have just seen, also we can combine this command with others to obtain counts that may interest us. For example, the ls command is used to list the entire contents of a directory. When piped with the wc -l command, it will also allow us to count the number of files and folders present in the directory.
ls /home/nombre-usuario | wc -l
In addition to all these options, you can get more information about this command running its help:
In these lines we have seen a basic use on the use of the WC command (word count) and its available options. Without forgetting the possibility of carrying out much more efficient tasks by combining the command wc with other Gnu / Linux commands. Now it is just a matter of looking for the moment in which this command can be useful to us.