Often you may want to use Bash to count the number of lines in each file in a specific directory.
You can use the following methods to do so:
Method 1: Count Number of Lines in Each File in Directory
wc -l c:/users/bobbi/data2/*
This particular example will return the file names along with the total number of lines in each file in the directory located at c:/users/bobbi/data2 on our computer. The total line count of all files will also be returned.
Method 2: Count Number of Lines in Specific Files in Directory
find c:/users/bobbi/data2 -name '*teams*' | xargs wc -l
This particular example will return the file names along with the total number of lines for only the files that contain the pattern “teams” in the filenames in the directory located at c:/users/bobbi/data2 on our computer. The total line count of all files will also be returned.
Note that both of these examples use the wc (“word count”) command with the -l parameter to specify that we’d like to count the number of lines in each file.
The following examples show how to use each method in practice.
Example 1: Count Number of Lines in Each File in Directory
We can use the following syntax to count the total number of lines in each file in a directory named data2:
wc -l c:/users/bobbi/data2/*
The following screenshot shows how to use this syntax in practice:

From the output we can see:
- The file bad_teams.txt has 2 lines.
- The file coaches.txt has 8 lines.
- The file good_teams.txt has 4 lines.
- The file great_teams.txt has 10 lines.
We can also see at the end of the output that there are 24 total lines among all files in this directory.
Example 2: Count Number of Lines in Specific Files in Directory
We can use the following syntax to count the total number of lines only in each file that contains the string “teams” in the filename in a directory named data2:
find c:/users/bobbi/data2 -name '*teams*' | xargs wc -l
The following screenshot shows how to use this syntax in practice:

The output displays the file names and total lines for each file that contains the pattern “teams” in the filename.
We can also see at the end of the output that there are 16 total lines among files that contain “teams” in the filename in this directory.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to List All Files in Folder by Size
Bash: How to List All Files in Folder Without Extension
Bash: How to List All Files Matching a Specific Pattern
