How to Count Files in a Directory on Linux

If you want to find out how many files are inside a folder before running any type of file processing script on it.

Using the find Command

The find command can locate files in a directory and its subdirectories. By piping the output to wc -l, you can count the number of files:

sh find /etc -type f | wc -l

This command:

Searches for all files (-type f) under /etc Pipes the list to wc -l to count the lines, which correspond to the number of files

Using the tree Command

The tree command provides a structured view of a directory and includes a summary with the total number of files and directories:

tree <directory>

At the bottom of the output, you'll see a summary showing the number of files and directories.