Count the number of words, lines, bytes, even characters in a file from the terminal
wc (word count) command is used in the Linux systems to count the number of words, lines, and bytes in a text file. You can pipe and use it in numerous ways with other commands to display information about text files pretty easily.
wc [options..] [file_name]
Options available with
|print number of lines in a file|
|print number of words in a file|
|print count of bytes in a file|
|print count of characters in a file|
|print length of the longest line in a file|
We will see a basic example explaining the use of the
wc command in Linux.
We have a demo file in place named test.txt. Following is the content of the demo file test.txt.
This is a demo file. This article willl help you with learning the wc command. wc command is pretty easy to use. You can learn about its features in this article. You can find many helpful articles related to all your Linux needs on this port$ End of file Thank you.
wc command on this file.
gaurav@ubuntu:~$ wc test.txt 11 51 275 test.txt gaurav@ubuntu:~$
In this output, you can see that the numbers represent the values as follows.
- number of lines — 11
- number of words —
- number of bytes —
You can also display the number of lines and words using the
wc command on multiple files.
wc /etc/passwd /proc/cpuinfo
gaurav@ubuntu:~$ wc /etc/passwd /proc/cpuinfo 55 95 3102 /etc/passwd 108 820 4688 /proc/cpuinfo 163 915 7790 total gaurav@ubuntu:~$
2nd line in the output displays the details about
/etc/passwd file and the 3rd line
/proc/cpuinfo. At the end of the output, the total figure of both the files combined is displayed.
How to Count Total Lines in a Text File
-l option of the
wc command, you can print the number of lines in a given text file.
wc -l [file_name]
wc -l /etc/group
gaurav@ubuntu:~$ wc -l /etc/group 81 /etc/group gaurav@ubuntu:~$
Here, in the output, we can see that the number of lines in the
/etc/group file is 81.
How to Count Words in a Text File
-w (lowercase) option in
wc command prints the total number of words in a text file in the terminal.
wc -w [file_name]
wc -w test.txt
There are 51 words in the text file test.txt.
Get the Byte Count of a File
You can use the
-c option with the
wc command to print the number of bytes in the file on your terminal.
Number of bytes utilized by a file tells us about the memory occupied by that text file.
wc -c [file_name]
wc -c /etc/passwd
gaurav@ubuntu:~$ wc -c /etc/passwd 3102 /etc/passwd gaurav@ubuntu:~$
From the output we can conclude that, the
passwd file utilizes 3102 bytes.
Get Total Number of Characters in a File
-m option with the
wc command will print the total number of characters in a given file.
wc -m [file_name]
wc -m test.txt
gaurav@ubuntu:~/space$ wc -m test.txt 275 test.txt gaurav@ubuntu:~/space$
The output shows that there are 275 characters in the given file.
Get Length of the Longest Line in a File
You can use the
-L (uppercase) option with the
wc command to print the length of the longest line in the text file. This command prints the length in terms of the number of characters in a line.
wc -L [file_name]
wc -L test.txt
This output represents that there are 82 characters in the longest line in the given text file test.txt.
How to Count Number of Text Files in the Current Directory
wc command can also be used to count the total number of text files in the current directory. In order to do this, you have to use
wc command with the —
find command through piping.
Let us look at this usage of
wc command through an example.
find . -type f | wc -l
. (dot) : Here, the
. (dot) means that
find command should search in the current directory.
-type: This specifies the
find command to look for similar file types in the current directory.
f represent ‘files’.
Whatever is the output of this first command
find will then be piped to the
wc will then count the total number of files in the current directory and display the number on your terminal.
gaurav@ubuntu:~/space$ find . -type f | wc -l 13 gaurav@ubuntu:~/space$
The output is displayed as 13 which means that there are 13 text file of the same type in the given directory.
As you’d agree that the usage of
wc command is pretty simple and can be easily used with your text files to get the details about them. This command can also be used in combination with other commands using the piping option.