How to Count Lines, Words, and More in Linux

Linux Count Lines Words Length Featured

Finding the number of lines, words, characters, and more in a file is mostly straightforward on Linux. Most, if not all, Linux distros come with built-in commands to display the various properties in a file. The wc command in Linux comes in handy whether you want to find the size of a file or count how many words or lines are in it. Let’s look at how you can count lines and words in Linux.

Good to know: Copying and pasting a lot on Linux? Here’s how to copy and paste text, files, and folders in Linux terminal.

Count Lines in a File

As a Linux command line tool, wc can print numerous properties of the file you specify. To keep things simple, open the directory where your file is located and launch a terminal window by right-clicking and bringing up the respective options.

Execute the following command to display the number of lines in a file called “theme.txt”:

wc -l theme.txt

Note: Replace “theme.txt” with the particular file in your case.

Linux Count Lines

The output will show you the number of lines in the file followed by the name of the file.

Tip: Want to supercharge your Linux distro? Here are 101 of the best software for your Linux machine.

Count Words in a File

If you want to know the number of words in a file containing an article or a document summary, run the wc command with the -w flag. Simply execute the following command in a terminal window:

wc -w theme.txt
Linux Count Words

Again, the format of the output remains the same; you can see the word count of the file followed by its name.

Count Characters in a File

To count the number of characters in a file, you can use the -m flag. This will not only count all the alphabets, numbers, and symbols in the file but also include things like spaces:

wc -m theme.txt
Linux Count Characters

Just like before, the output will display the count of characters followed by the filename.

Tip: the Linux terminal can do a lot more than counting lines, words, and characters in a file, such as controlling time, date, and more using timedatectl.

Count Bytes in a File

If you’re interested to know the size of a file instead of its internal properties, run the wc command with the -c flag:

wc -c theme.txt
Linux Count Bytes

In this case, the number of bytes is the same as the number of characters in the file. But, it can also be different in some cases.

FYI: Need smaller files? Here are the 12 best compression tools to compress files on Linux.

Display Lines, Words, and Bytes in a File

There’s also a way to print the lines, words, and bytes in a file together in a single line. The wc command displays this when you just type the filename without passing any parameter. Execute the following command to do so:

wc theme.txt
Linux Wc Display All Info

As you can see, the output shows three fields in the same line – 14 (lines in the file), 385 (words in the file), and 2320 (bytes in the file).

Tip: Curious how to get things done faster? Here’s a beginner’s guide to shell scripting in Linux.

Find the Length of the Longest Line in a File

You might be feeling adventurous and want to find out how long is the longest line in your file. Type the following command to display the length of the longest line in your file:

wc -L theme.txt
Linux Count Length Of Longest Line

The output shows the size of the longest line in your specified file.

Display wc Command Options

Finally, there’s also a way to print all the variations of the wc command for easy reference. You just need to execute the following command to display all of the commands we discussed above:

wc --help
Linux Wc Display Help

As you can see, the output clearly shows the parameters (and their alternate options) you can use to display the various details of a desired file.

Linux Terminal and Useful Commands

The wc command in Linux makes it incredibly easy to find out the number of lines, words, characters, or bytes in a Linux file. It’s one of the most useful Linux commands for new users who are just dipping their toes in the world of the best Linux distros. If you’re switching to Linux from a Mac, you might be interested in the best Linux distros for Mac users. Also, if you’re looking for ways to display the contents of your Linux directories, there are 15 Linux LS commands you need to know.

Image credit: count word in a dictionary by 123RF

Is this post useful?
Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Tanveer Singh
Tanveer Singh - Staff Writer

After a 7-year corporate stint, Tanveer found his love for writing and tech too much to resist. An MBA in Marketing and the owner of a PC building business, he writes on PC hardware, technology, video games, and Windows. When not scouring the web for ideas, he can be found building PCs, watching anime, or playing Smash Karts on his RTX 3080 (sigh).