Linux Commands

Count Files in Directory Recursively

Sometimes, it’s necessary to figure out the exact number of files available under a specific directory. The problem arises if the directory contains one or more sub-directories. Depending on the number of files and directories, manual counting can be virtually impossible.

In this guide, check out how to count files in a directory recursively in Linux.

File counting

Basic file counting
For demonstration, here’s a sample directory with multiple sub-directories.

$ tree demo_dir

As you can see, the tree command will print the entire directory structure recursively with the number of files at the end. However, if the number of files and directories is too large, getting a report is inefficient.

An alternative way is to use the find and wc commands. First, the find command will generate a list of files within the directory. Then, the wc command will count the line of output, determining the number of files.

The command will look something like this.

$ find <directory> -type f | wc -l

In the case of the find command, here’s a short explanation of the flags and arguments.

  • <directory>: The directory to perform the file count on.
  • -type f: Determines the type of file (file/directory) to look for. Here, “f” signifies for files only.

In the case of the wc command, here’s a short explanation of the flag.

  • -l: Counts the number of lines. It works by counting the number of newline characters in the output.

Let’s apply the command to the test directory of ours.

$ find ./demo_dir -type f | wc -l

If possible, it’s recommended to use the full path of the directory.

$ find /home/viktor/Desktop/demo_dir -type f | wc -l

Counting with directories
If directories are also to be included in the counting, use the following command structure instead. The find command will print the directories and the subsequent files in the output.

$ find <directory> | wc -l

Directory depth
The find command supports directory depth. Directory depth determines how deep find will descend in search for files.

There are two types of directory depths that find support.

  • maxdepth: The maximum level find will descend. The value of maxdepth will be a non-negative integer.
  • mindepth: The minimum depth required for find to act on a directory. The value of mindepth will be a non-negative integer.

Let’s have a look at these values in action. The find command structure would look like this.

$ find <directory> -maxdepth <maxdepth_value>

$ find <directory> -mindepth <mindepth_valule>

Counting Files Using GUI

If you have the option to use GUI to check for file count, we can count the files in a directory using file managers. File managers let users manage the files and directories elegantly. Any file manager supports all the basic functions like searching, copying, moving, creating, and deleting files. Some file managers even support advanced features like SSH connections.

Here are some of the best file managers available for Linux. Most of them should be available for all the popular Linux distros.

Nautilus File Manager
It’s the default file manager of the GNOME desktop. It has a very simplistic UI, easy navigation, and management.

Check out Nautilus File Manager.

Konqueror File Manager
Konqueror is the default manager that comes with the KDE desktop. It’s has a simplistic file manager with additional features, such as FTP/SFTP support, smb (Windows) shares, audio ripping, etc.

Konqueror uses the KHTML rendering engine. Check out Konqueror.

Dolphin File Manager
Dolphin replaces Konqueror as the default file manager on the KDE desktop. It’s a free, open-source, lightweight file manager that aims at simplicity, flexibility, and full customization. It allows users smooth browsing, locating, copying, and moving files experience around the Linux system. It incorporates other interesting features like file previewing, tabbed navigation, file sorting, and grouping, etc.

Check out Dolphin.

SpaceFM File Manager
Unlike the other file managers described, SpaceFM is a standalone file manager that’s not related to any desktop environment. It’s a beautiful file manager available for all popular Linux distros. It features bash integration, built-in VFS, and menu customization, etc.

Check out SpaceFM.

GNU Midnight Commander
Finally, GNU Midnight Commander is a file manager for the command line. It  is a full-fledged file manager but on the console screen. It supports all the classic functions like searching, copying, moving, and deleting files, etc.

Check out GNU Midnight Commander.

Final Thoughts

Counting files on Linux isn’t difficult at all. All it requires is having the right tool and knowledge to figure out. Hopefully, this guide was successful in demonstrating how to count files in directories recursively in Linux.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.