• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Linux Tips / Count Files / How to Count the Number of lines, Words, and, Characters in a Text File from Terminal?

How to Count the Number of lines, Words, and, Characters in a Text File from Terminal?

August 2, 2011 by cmdlinetips

word count in termimal
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

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailTen Linux Commands on “tar” and “gzip” You Need to Know Default ThumbnailHow to Run a Shell Command from Python and Get The Output? Default ThumbnailHow to join PDF files in Mac on Terminal? How to Get a Specific Line or Lines From a Big File? Hint: Use One Liner AWK

Filed Under: Count Files, Count Lines, Linux Tips Tagged With: Count Files, Count Lines, Linux Tips, wc

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version