What command is used to count the total number of lines, words, and characters contained in a file

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.

# wc [options] filenames

The following are the options and usage provided by the 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.

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.

[[email protected] ~]# cat tecmint.txt Red Hat CentOS Fedora Debian Scientific Linux OpenSuse Ubuntu Xubuntu Linux Mint Pearl Linux Slackware Mandriva

1. A Basic Example of WC Command

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.

[[email protected] ~]# wc tecmint.txt 12 16 112 tecmint.txt

2. Count Number of Lines

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.

[[email protected] ~]# wc -l tecmint.txt 12 tecmint.txt

3. Display Number of Words

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.

[[email protected] ~]# wc -w tecmint.txt 16 tecmint.txt

4. Count Number of Bytes and Characters

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

[[email protected] ~]# wc -c tecmint.txt 112 tecmint.txt [[email protected] ~]# wc -m tecmint.txt 112 tecmint.txt

5. Display Length of Longest Line

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.

[[email protected] ~]# wc -L tecmint.txt 16 tecmint.txt

6. Check More WC Options

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

[[email protected] ~]# 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. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts -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 [email protected] 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'

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

What command is used to count the total number of lines, words, and characters contained in a file

We are thankful for your never ending support.

What command is used to count the total number of lines, words, and characters contained in a file
word count in termimal

How can we get the number of lines or number of words in a file? The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal.

The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.

To count the number of lines, use “wc” with “l” as

wc -l yourTextFile

To count the number of words, use “wc” with “w” option as

wc -w yourTextFile

And to count the total number of characters, use “wc” with “c” as

wc -m yourTextFile

Using wc with no options will get you the counts of bytes, lines, and words (-c, -l and -w option).

>wc file1.txt 1065 5343 40559 file1.txt

Count words, characters, and lines in multiple files

wc command can take multiple files at the same time and give you the number of words, characters, and lines. To get counts from multiple files, you simply name the files with space between them. Also it will get you the total counts. For example, to count the number of characters (-m), words (w) and lines (-l) in each of the files file1.txt and file2.txt and the totals for both, we would simply use

>wc -mlw file1.txt file2.txt

We would get the results in a nice tabular form

1065 5343 40454 file1.txt 296 1075 11745 file2.txt 1361 6418 52199 total

How to Count a Certain Type of Files in a Directory?

One can also cleverly use the “wc” command on terminal and find the number of files (or files of certain type) in a directory. For example, to find the number of pdf files in a directory

ls -l *.pdf | wc -l

And remember that the first line of “ls -l” statement is a description. Therefore, the total number of pdf files is one less than the result of “ls -l *.pdf | wc -l“.

We just saw an example of using pipe operator “|” to count files. Here we fed the output of command “ls -l *.pdf” to “wc”. Or ability to piping (or chaining) multiple commands is a hallmark of Linux. We can do the same to numerous scenarios. For example, if we want to count all users who have currently logged on, we can do

who | wc -l

How to get the number of lines matching a pattern in a file?

If you want to count the number of lines matching a string pattern in a text file, the “grep” command with the option “-c’ comes in really handy.

less my_text_file.txt | grep -c "pattern"

or we can directly apply “grep” on the file like

grep -c "pattern" my_text_file.txt