How to count files in a directory and its subdirectories in linux

How to Count Files in a Directory and its Subdirectories in Linux

While navigating your Linux file system, you may come across directories containing a lot of files. Sometimes you need to find the number of files in those directories and their subdirectories in a Linux system. As manual counting is not a practical solution in this case. Therefore in this post, we will look at some other quick methods to count files in a directory and its subdirectories in a Linux OS.

Method#1 Using the find Command

The find command combined with the wc command can help you count the files recursively. The find command finds and lists all the files in a directory and its subdirectories and then the wc command counts the number of files. This way you can get the count of files in a directory recursively.

Sponsored

Here is the syntax to count only the files in a directory recursively:

$ find  -type f | wc -l

For instance, to count all the files in the current directory, the command would be:

$ find -type f | wc -l

How to count files in a directory and its subdirectories in linux 1

Similarly, to count only the files in the ‘Documents’ directory, the command would be:

$ find ~/Documents -type f | wc -l

How to count files in a directory and its subdirectories in linux 2

To count only the subdirectories within a directory, use –type d as follows:

$ find  -type d | wc -l

How to count files in a directory and its subdirectories in linux 3

To include both the files and the subdirectories in the count, use the command below:

$ find  | wc -l

The find command also enables you to confine your search to specific directory levels. Below is the directory tree of our Documents directory.

How to count files in a directory and its subdirectories in linux 4

Now, for instance, if you want to count the files in a specific directory up to 3 levels (up to ‘mydocs’ directory), use the command line option ‘-maxdepth 3’:

$ find  -maxdepth 3 -type f| wc -l

This command will count the files up to 3 levels with the top level being the ‘Documents’ directory.

How to count files in a directory and its subdirectories in linux 5

Similarly, you can also start the count from a specific level. For instance, if you want to exclude the first 2 directories from the file count, you can use the command line option ‘-mindepth 2’. The ‘-mindepth 2’ will tell the find command to go 2 levels down before starting the search.

$ find  -mindepth 2 -type f | wc -l

How to count files in a directory and its subdirectories in linux 6

Method#2 Using the ls Command

The ls command in Linux is used for listing files and directories. Using the ls with the wc command, we can get the count of files in a specific directory. However, note that this count does not include the files inside the subdirectories.

Sponsored

To find the number of files in a directory, pass its output to the wc command as follows:

$ ls  | wc -l

If you do not specify the directory path, it will count files in the current working directory,

Here is the output of the ls command in our ‘Documents’ directory.

How to count files in a directory and its subdirectories in linux 7

Now to count the number of files in the ‘Documents’ directory, the command would be:

$ ls -a  | wc -l

The ls command will list the files in the specified directory while the wc command will count the number of files. So in the output, you will get the number of files including the subdirectories under the specified directory. Here note that this count will not be recursive as it will not count the files under the subdirectories.

To exclude subdirectories and count only the files in a directory, use the command below:

$ ls -p  | grep -v / | wc -l

How to count files in a directory and its subdirectories in linux 8

To count the hidden files too, use the command below:

$ ls -Ap  | grep -v / | grep "^." | wc -l

How to count files in a directory and its subdirectories in linux 9

Method#3 Using the tree Command

The tree command also tells you the count of files and subdirectories under a directory. To count the files and directories under a specific directory, use the command below:

$ tree  | tail -1

In the output, you will find the count of files and directories under the specified directory.

How to count files in a directory and its subdirectories in linux 10

To include the hidden files too, use the -a flag with the tree command as follows:

$ tree -a  | tail -1

How to count files in a directory and its subdirectories in linux 11

That’s all! In this post, we have gone through how to count files in a directory and its subdirectories on Linux OS. We have discussed three different methods to count files in a directory which include the ls, find and tree commands.

Karim buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

See also  How to Fix "Unable to Locate Package openjdk-8-jdk" in Ubuntu Server

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply