Linux - Count number of files in a Directory

less than 1 minute read

Get the number of files

To get the number of files in a directory, including sub-directories:

#!/bin/bash
ls -lR |grep ^- |wc -l

What this does is a recursive directory search, using the long listing format. The lines listing directories will begin with a d so piping to grep and searching for lines that begin with - will list only the files. The output is piped into wc which uses the option to print the newline counts.