wc (short for word count) is a command line tool in Unix/Linux operating systems, which is used to find out the number of newline count, word count, byte and character count in the files specified by the File arguments to the standard output and hold a total count for all named files.
When you define the File parameter, the wc command prints the file names as well as the requested counts. If you do not define a file name for the File parameter, it prints only the total count to the standard output.
In this article, we will discuss how to use the wc command to calculate a file’s newlines, words, characters, or byte count with practical examples.
wc Command Syntax
The syntax of the wc command is shown below.
# wc [options] filenames
The followings are the options and usage provided by the wc command.
wc -l
– Prints the number of lines in a file.wc -w
– prints the number of words in a file.wc -c
– Displays the count of bytes in a file.wc -m
– prints the count of characters from a file.wc -L
– prints only the length of the longest line in a file.
Let’s see how we can use the ‘wc‘ command with the 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 tecmint.txt file using the cat command as shown below.
$ cat tecmint.txt Red Hat CentOS AlmaLinux Rocky Linux Fedora Debian Scientific Linux OpenSuse Ubuntu Xubuntu Linux Mint Deepin Linux Slackware Mandriva
1. A Basic Example of WC Command
The ‘wc‘ command without passing any parameter will display a basic result of the ‘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.
$ wc tecmint.txt 12 16 112 tecmint.txt
2. Count Number of Lines in a File
Count the number of newlines in a file using 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 field is assigned as count and the second field is the name of the file.
$ wc -l tecmint.txt 12 tecmint.txt
3. Count Number of Words in a File
The -w
argument with the wc command prints the number of words in a file. Type the following command to count the words in a file.
$ wc -w tecmint.txt 16 tecmint.txt
4. Count Number of Characters in a File
When using option -m
with the wc command will print the total number of characters in a file.
$ wc -m tecmint.txt 112 tecmint.txt
5. Count Number of Bytes in a File
When using option -c
will print the number of bytes of a file.
$ wc -c tecmint.txt 112 tecmint.txt
6. Display Length of Longest Line in File
The ‘wc‘ command allows an argument ‘-L
‘, it can be used to print out the length of the longest (number of characters) line in a file.
So, we have the longest character line (‘Scientific Linux‘) in a file.
$ wc -L tecmint.txt 16 tecmint.txt
7. Check wc Command Options
For more information and help on the wc command, simply run the ‘wc --help
‘ or ‘man wc
‘ from the command line.
$ wc --help OR $ man wc
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. A word is a non-zero-length sequence of characters delimited by white space. With no FILE, or when FILE is -, read standard input. 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 maximum display width -w, --words print the word counts --help display this help and exit --version output version information and exit GNU coreutils online help: <https://www.gnu.org/software/coreutils/> Full documentation at: <https://www.gnu.org/software/coreutils/wc> or available locally via: info '(coreutils) wc invocation'
In this article, you’ve learned about the wc command, which is a simple command-line utility to count the number of lines, words, characters, and byes in text files. There are lots of such other Linux commands, you should learn and master your command-line skills.
Is there a way to see how many times the word ‘the’ was used?
@Thomas,
To count occurrence of word in Linux text file, you should use following command.
The previous
cut | grep | wc
still only counts lines, not word occurrences, and would consider “anthem,” “these,” etc matches for the substring “the.”This is a way to do what you want-
@Ravi,
Thanks..but I am getting count from this command as wc -l <filename.txt
How we can subtract 2 from this count ?
How we can subtract 2 from count?
@Pratab,
Please go through this article: 5 Useful Ways to Do Arithmetic in Linux Terminal
I’ve been using wc -l ‘find *.csv -type f’ for some time, but I would like to reduce the count of each file by 1 to not include the header records. Is this possible?
Thank you sir, very helpful article.
Help,
This all looks very straightforward if you know what you’re doing. Whereabouts do I type in the command
wc -w
to try and find out how many words are in a document, please?I’d greatly appreciate step by step from the beginning, pics would be good :)
@Ezza,
Here is the example command of how wc -c works.
Here 76522 are number of words in a document w_syst02.pdf.
Can you help me…this is the code I’ve made, but I want to count the lines of 01:19:21.
How to write the code just to count the all 01:19:21 lines????
11:01:11
03:53:28
01:19:21
01:19:21
01:19:21
01:19:21
01:19:21
01:19:21
01:19:21
01:19:21
The command is attempting to filter and count the lines containing the string “01:19:21” in a text file or a stream of text.
@TTT
If you want to count the occurrences of the line “01:19:21” in a text file using a Linux command, you can use the following command:
This will output the number of lines in the file that contain “01:19:21”.
Write a shell script to count the number of lines, characters, words in a file (without the use of commands). Also delete the occurrence of word “Linux” from the file wherever it appears and save the results in a new file.
anybody plz help
It is really helpful now. If I have any type of query , I will definitely ask u. Thanks for now.
what is the special purpose for linux?
How do I get no of records of the file exclude footer?
@Hasan,
Your question is incomplete, could you explain your question in more detailed fashion, so that we can understand better and give you a proper solution..
use linux
The tool for excelency is a computer (smartphone, tablet, etc) and need a software in special to work, to interactive with you like user. The best option to handle that is Linux. That is the reason.
cat filename.txt | grep -v “saerch” | sed -n ‘ $ p ‘
If u don’t want to print to print the last line
Go for grep -v option
can try grep -v ‘^6’
When I do a wc -l in a directory then the output is something like –
1 File1.txt
2 File2.txt
3 File3.txt
6 Total
I do not want to display the last Total. How can we do that? Any insights..!
You can try this
wc file*|grep -v total
grep with -v option removes the lines which have the word “total” in them., so except last line all files with word count will shown. Hope it helps
Please tell me how to display number of words in each line by using wc command?
while read line; do echo “$line” | wc -m ;done < filename
if your file contains '/' then use 'read -r' in the above command
Another simple way without wc is
awk '{print length($0) }' filename