EDDYMENS

Published a year ago

Linux & MacOS: Counting Files In A Directory

You can count the number of files in a directory with the wc command [↗].

wc outputs the total number of newlines, bytes, and words.

To count the number of files and folders in a directory you need to pipe the ls command to the wc command :

01: ls | wc -l 02: # example output: 115

The -l flag specifies that we only care about newline count. Which is a count of all files and folders listed out by the ls command.

NB: Since wc counts output on the screen, if your directory contains both folders and files, the folders will also be counted.

Here is another article you might like 😊 Matching markdown and HTML headings using regex (JS)