Recursively Count Files in a directory in Linux

In this article, we will learn to count files in a directory and in its sub directories recursively in Linux.

Table Of Contents

Introduction

Sometimes we come across a requirement to find the count of all files in a directory and in all it’s sub-directories. Manually opening each folder and counting the files would be a tedious task. Linux operating system has provided different commands to make this work easier. Lets look at various commands that help us to count the files recursively in Linux.

Using tree command

We can use the tree command to view the directory and sub-directory structure in Linux operating system. We can use same command to view the count of files as well. Lets look at it’s syntax,

tree <directory_path>

It will print the whole directory structure at the given path, in a tree format. Also, in the last line it will print total number of directories in the tree and total files count.

Advertisements

Examples:

If we want to the get count of all files in a directory “www” and its sub-directories, then use the below command,

tree www

Output:

  www
└── web
    ├── css
    │   └── main.css
    ├── html
    │   └── index.html
    └── script

4 directories, 2 files

If we want to include hidden files also in the count, then use -a option with the tree command, as shown below,

tree -a <directory_path>

Examples:

If we want to get count of all files including hidden files in a directory “www” and in all its sub-directories, then use the below command,

tree -a www

Output:

www
└── web
    ├── css
    │   └── main.css
    ├── html
    │   ├── index.html
    │   └── .local
    └── script

4 directories, 3 files

But this command will be inefficient if the directory is too large or has so many sub-directories. In those cases please use the next option.

Using the find and wc command

We can use the find command along with wc command to recursively count the files in a directory and all it’s sub-directories. Let’s look at it’s syntax,

find <directory_path> -type f | wc -l

Parameters:

directory_path   : path of the directory to perform count operation
-type f          : look for only files within the directory
-l               : counts the number of lines

Examples:

If we want to get count of all files in a directory “www” and it’s sub-directories, then use the below command

find www -type f | wc -l

Output:

3

If we want to count the files including directories then we can use the below command,

find <directory_path> | wc -l

Examples:

The below command can be used to get count of all files in folder “www”, including directories

find www | wc -l

Output:

8

If we want to count the files up to a specific level like 3 level in a directory tree then we can use the below command,

find <directory_path> -maxdepth 3 -type f | wc -l

Examples:

The below command can be used to get count of all files in directory “www”, including directories,

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

Output:

3

Sometimes when we try to count number of files in a directory, we might get some permission denied errors. In those cases we can use the below command to count only the files that we have access to. The error messages will be redirected to /dev/null

Syntax:

find <directory_path> -type f 2> /dev/null | wc -l

Examples:
The below command can be used to get count of all files in folder “www”, including directories

find www -type f 2> /dev/null | wc -l

Output:

3

Using locate command:

Even though locate command will not come as pre-installed in most of the Linux operating systems, we can use the command in debian based linux operating systems,

sudo apt-get install mlocate

or

sudo yum install mlocate

in RHEL based linux operating systems. After installing, please use the below syntax to recursively count the files in a directory.

Syntax:

locate -c <directory_path>

Examples:

If we want to get count of all files in a directory “www” and it’s sub-directories, then use the below command,

locate -c www

Output:

8

Using rsync command

Even though rsync is used for copying files from local to remote system, We can use rsync command to find all files, directories and symbolic links in a directory with –dry-run option

Syntax:

rsync --stats --dry-run -ax <directory_path> <dummy_directory>
  • dry-run : This will be used to let the rsync command not to transfer the files

Examples:

If we want to get count of all files in a directory “www” and it’s sub-directories, then use the below command

rsync --stats --dry-run -ax www dummy

Output:

Number of files: 8 (reg: 3, dir: 5)
Number of created files: 8 (reg: 3, dir: 5)
Number of deleted files: 0
Number of regular files transferred: 3
Total file size: 35 bytes
Total transferred file size: 35 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 0
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 272
Total bytes received: 45

sent 272 bytes  received 45 bytes  634.00 bytes/sec
total size is 35  speedup is 0.11 (DRY RUN)

Summary:

In this guide, we have learnt multiple options to recursively count the files in a directory and all it’s sub-directories in Linux. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top