- 6 Wc Command to Count Number of Lines, Words, and Characters in File
- wc Command Syntax
- 1. A Basic Example of WC Command
- 2. Count Number of Lines in a File
- 3. Count Number of Words in a File
- 4. Count Number of Characters in a File
- 5. Count Number of Bytes in a File
- 6. Display Length of Longest Line in File
- 7. Check wc Command Options
- 5 Ways to Count the Number of Lines in a File
- 1. Count Number Of Lines Using wc Command
- 2. Count Number Of Lines Using Awk Command
- 3. Count Number Of Lines Using Sed Command
- 4. Count Number Of Lines Using Grep Command
- 5. Count Number Of Lines Using nl and cat Commands
- How to Count lines in a file in UNIX/Linux
- Using “wc -l”
- Using awk
- Using sed
- Using grep
- Some more commands
- count lines in a file
6 Wc Command to Count Number of Lines, Words, and Characters in File
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.
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.
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: Full documentation at: 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.
5 Ways to Count the Number of Lines in a File
On Linux, you can do a single task in several ways. Likewise, if you want to count the number of lines in single or multiple files, you can use different commands. In this article, I’ll share five different ways including that you can use to print a total number of lines in a large file.
1. Count Number Of Lines Using wc Command
As wc stands for “word count“, it is the most suitable and easy command that has the sole purpose of counting words, characters, or lines in a file.
Let’s suppose you want to count the number of lines in a text file called distros.txt.
You can use «-l» or «—line» option with wc command as follows:
You can see that wc also counts the blank line and print the number of lines along with the filename. In case, you want to display only the total number of lines, you can also hide the filename by redirecting the content of the file to wc using a left-angle bracket (<) instead of passing the file as a parameter.
Moreover, to display a number of lines from more than one file at the same time, you need to pass the filenames as arguments separated by space.
$ wc --lines distros.txt distros.txt distros.txt
In another way, you can also make use of the cat command to redirect the file content to the wc command as input via pipe (‘|’) .
Though it will also count the number of lines in a file, here the use of cat seems redundant.
2. Count Number Of Lines Using Awk Command
Awk is a very powerful command-line utility for text processing. If you already know awk, you can use it for several purposes including counting the number of lines in files.
However, mastering it may take time if you’re at a beginner level. Hence, if you just want to use it to count the total number of lines in a file, you can remember the following command:
Here, NR is the number of records or say line numbers in a file being processed at the END section.
3. Count Number Of Lines Using Sed Command
Sed is a also very useful tool for filtering and editing text. More than a text stream editor, you can also use sed for counting the number of lines in a file using the command:
Here, ‘=’ prints the current line number to standard output. So, combining it with the -n option, it counts the total number of lines in a file passed as an argument.
4. Count Number Of Lines Using Grep Command
Using yet another useful pattern search command grep, you can get the total number of lines in a file using ‘-e’ or ‘—regexp’ and ‘-c’ or ‘—count’ options.
$ grep -e "$" -c distros.txt Or $ grep -e "^" -c distros.txt
Here, ‘$’ is a regular expression representing the end of a line and the ‘^’ start of a line. You can use either of the regular expression.
5. Count Number Of Lines Using nl and cat Commands
Instead of directly getting the total no of lines, you can also print the file content and get the total number of lines by peeking at the last line number. For such purpose, nl is a simple command to print data with numbered lines.
For large files, it does not seem like a suitable method to display all data in a terminal. So what you can also do is pipe the data to tail command to just print only some of the last numbered lines.
Likewise, a cat command with ‘-n’ can also be used to print file content with line numbers.
$ cat -n distros.txt | tail -n1
Conclusion
After learning five ways to count a number of lines, you must be wondering which is the best way for you? In my opinion, whether you’re a beginner or advanced user of Linux, the wc command is the easiest and fastest approach.
However, if you’re in process of learning other powerful tools like grep, awk, and sed, you can also practice these commands for such purpose to hone your skills.
How to Count lines in a file in UNIX/Linux
Question: I have a file on my Linux system having a lot of lines. How do I count the total number of lines in the file?
Using “wc -l”
There are several ways to count lines in a file. But one of the easiest and widely used way is to use “wc -l”. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output.
So consider the file shown below:
$ cat file01.txt this is a sample file with some sample data
1. The “wc -l” command when run on this file, outputs the line count along with the filename.
$ wc -l file01.txt 5 file01.txt
2. To omit the filename from the result, use:
3. You can always provide the command output to the wc command using pipe. For example:
You can have any command here instead of cat. Output from any command can be piped to wc command to count the lines in the output.
Using awk
If you must want to use awk to find the line count, use the below awk command:
Using sed
Use the below sed command syntax to find line count using GNU sed:
Using grep
Our good old friend «grep» can also be used to count the number of lines in a file. These examples are just to let you know that there are multiple ways to count the lines without using «wc -l». But if asked I will always use «wc -l» instead of these options as it is way too easy to remember.
With GNU grep, you can use the below grep syntax:
Here is another version of grep command to find line count.
$ grep -Hc ".*" file01.txt file01.txt:5
Some more commands
Along with the above commands, its good to know some rarely used commands to find the line count in a file.
1. Use the nl command (line numbering filter) to get each line numbered. The syntax for the command is:
$ nl file01.txt 1 this is a sample 2 file 3 with 4 some sample 5 data
Not so direct way to get line count. But you can use awk or sed to get the count from last line. For example:
$ nl file01.txt | tail -1 | awk '' 5
2. You can also use vi and vim with the command «:set number» to set the number on each line as shown below. If the file is very big, you can use «Shift+G» to go to the last line and get the line count.
3. Use the cat command with -n switch to get each line numbered. Again, here you can get the line count from the last line.
$ cat -n file01.txt 1 this is a sample 2 file 3 with 4 some sample 5 data
$ cat -n file01.txt | tail -1 | awk '' 5
4. You can also use perl one lines to find line count:
$ perl -lne 'END < print $. >' file01.txt 5
count lines in a file
The standard way is with wc , which takes arguments to specify what it should count (bytes, chars, words, etc.); -l is for lines:
$ wc -l file.txt 1020 file.txt
How do I count the lines in a file if I want to ignore comments? Specifically, I want to not count lines that begin with a +, some white space (could be no white space) and then a %, which is the way comment lines appear in a git diff of a MATLAB file. I tried doing this with grep, but couldn’t figure out the correct regular expression.
@Gdalya I hope the following pipeline will do this (no tests were perfomed): cat matlab.git.diff | sed -e ‘/^\+[ ]*.*\%$/d’ | wc -l . /regexp/d deletes a line if it matches regexp , and -e turns on an adequate (IMNSHO) syntax for regexp .
@celtschk , as long as this is usual in comment lines: is it possible to modify your grep command in order to consider as comment cases like » + Hello» (note the space(s) before the + )?
@SopalajodeArrierez: Of course it is possible: grep -v ‘^ *+’ matlab.git.diff | wc -l (I’m assuming the quote signs were not actually meant to be part of the line; I also assume that both lines with and without spaces in front of the + are meant to be comments; if at least one space is mandatory, either replace the star * with \+ , or just add another space in front of the star). Probably instead of matching only spaces, you’d want to match arbitrary whitespace; for this replace the space with [[:space:]] . Note that I’ve also removed matching the % since it’s not in your example.