How To Count Files in Directory on Linux
Count Files using wc
The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command.
$ ls | wc -l
$ ls /etc | wc -l
Count Files Recursively using find
In order to count files recursively on Linux, you have to use the “find” command and pipe it with the “wc” command in order to count the number of files.
$ find <directory> -type f | wc -l
Count Files using tree
An easy way of counting files and directories in a directory is to use the “tree” command and to specify the name of the directory to be inspected.
$ tree <directory>
3 directories, 3 files
In order to count hidden files using tree, you have to execute “tree” and append the “-a” option for “all”, followed by the directory to be analyzed.
$ tree -a <directory>
For example, if we count files and directories in your “/home” directory, you will be able to see that there is a difference because multiple hidden files are present.
$ tree /home/user
5319 directories, 27049 files
$ tree -a /home/user
7389 directories, 33635 files