


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. How to get the number of lines matching a pattern in a file? For example, if we want to count all users who have currently logged on, we can do We can do the same to numerous scenarios.

Or ability to piping (or chaining) multiple commands is a hallmark of Linux. Here we fed the output of command “ls -l *.pdf” to “wc”. We just saw an example of using pipe operator “|” to count files. Therefore, the total number of pdf files is one less than the result of “ ls -l *.pdf | wc -l“. For example, to find the number of pdf files in a directoryĪnd remember that the first line of “ls -l” statement is a description. One can also cleverly use the “ wc” command on terminal and find the number of files (or files of certain type) in a directory. How to Count a Certain Type of Files in a Directory? We would get the results in a nice tabular form 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 To get counts from multiple files, you simply name the files with space between them. Wc command can take multiple files at the same time and give you the number of words, characters, and lines. Count words, characters, and lines in multiple files
