As a system administrator, you are probably monitoring the disk space on your system all the time. When browsing directories on your server, you might have come across directories with a lot of files in them. Sometimes, you may want to know how many files are sitting in a given directory, or in many different directories. In other words, you want to count the number of files that are stored in a directory on your system.
In this article, we will show you several different ways to find the number of files in a directory in Linux.

1. Count Files in Directory Using wc command

WC command, short for Word Count, is a command line tool in Unix/Linux systems used for printing newlines, counting number lines & characters in a file. The command can also be combined with other piping operations for general counting functions.
To count the number of files in a directory, use the syntax below:

$ ls -1U DIR_NAME | wc -l

The command above will give you a sum of all files, including directories and symlinks. The -1 option means list one file per line and -U tells ls to do not sort the output which makes the execution of the command faster.
ls -1U command doesn’t count hidden files (dotfiles).
Example:

$ ls -1U /etc | wc -l
254


If you want to count only files and not include the directories use the following:

$ ls -1Up DIR_NAME | grep -v / | wc -l

The -p option forces ls to append slash (/) indicator to directories. The output is piped to the grep -v command that exclude the directories.
Example:

$ ls -1Up /etc | grep -v / | wc -l
103

To have more control over what files are listed, use the find command instead of ls:

$ find DIR_NAME -maxdepth 1 -type f | wc -l

-type f option tells find to list only files (including dotfiles), and -maxdepth 1 limit search to the first-level directory.
Example:

$ find /etc -maxdepth 1 -type f | wc -l
98

2. Count Files in Directory Using tree command

tree is a Unix/Linux command line tool that recursively prints directories in a tree-like format. It displays each directory along with any subdirectories within it. In addition, it can also display and print the number of files in a directory.Navigate into our sample ‘/etc’

$ cd /etc

Then, run the tree command

$ tree

Output:

.................

402 directories, 2778 files

As you can see, the number of files and directories is available at the bottom of the tree command.

3. Count files recursively through directories and Subdirectories

To recursively count files through directories and subdirectories using the command below

$ find DIR_NAME -type f | wc -l


Where:
DIR_NAME is the directory name
- type f specifies files only
wc (Word Count) counts newlines, words, and bytes on its input
-l Counts new lines
If you are counting files in the current directory replace the DIR_NAME with a period as shown

$ find . -type f | wc -l

To include other subfolders and files within subfolders, leave out the – type f flag.

$ find . | wc -l

Conclusion

We have shown you how to count files in directory using the ls, wc, find and tree commands.
If you have any questions or feedback, feel free to leave a comment.