Open In App

How to Count Files in Directory Recursively in Linux

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When exploring directories on your server, you may have come across folders with a large number of files in them. You might wish to know how many files exist in a certain directory or across many directories at times. To put it another way, you wish to count the number of files saved in a directory on your system. In this article, we are going to see how you can easily count files in a directory on Linux.

There are 7 different methods for Counting Files in Directory Recursively in Linux:

  • Method 1: Count files using wc
  • Method 2: Basic file counting
  • Method 3: Count files recursively using the find command
  • Method 4: Counting with directories
  • Method 5: Directory depth
  • Method 6: Counting hidden files with the tree command
  • Method 7: Counting files using GUI

Let’s go through all the methods one by one.

Method 1: Count files using wc

On Linux, the ls command, piped with the wc -l command, is the simplest way to count files in a directory.

ls | wc -l

The wc command is used in Linux to output the number of bytes, characters, or newlines. In this situation, though, we are using this command to count the number of files in a directory.

Assume you wish to count the number of files in the /etc directory.

To do this, run the “ls” command on the “/etc” directory and pipe it into the “wc” command.

ls /etc | wc -l

 

Method 2: Basic file counting

Using the “tree” command and specifying the name of the directory to be inspected is another simple approach to counting files and directories in a directory.

tree <directory>

 

Method 3: Count files recursively using the find

To count files recursively on Linux, use the find command and pipe it with the wc command to count the number of files.

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

Here’s a brief explanation of the options and parameters for the find command.

  • <directory>: The directory on which to execute the file count.
  • -type f: Specifies the file (file/directory) type to look for. The letter “f” stands for “files only.”

Let’s apply the command to the /etc directory of ours.

find /etc -type f | wc -l

 

Method 4: Counting with directories

If directories are to be counted as well, use the following command structure instead.

find <directory> | wc -l

 

Method 5: Directory depth

Directory depth is supported by the find command. The depth of the directory controls how far locate will search for files.

There are two kinds of support directory depths.

  • maxdepth: The maximum level will be found. The value of maxdepth will be an integer that is not negative.
  • mindepth: The minimal depth at which find can function on a directory. The value of mindepth will be an integer that is not negative.

Let us examine these values in action. The structure of the find command would be as follows.

find <directory> -maxdepth <maxdepth_value> | wc -l

 

find <directory> -mindepth <mindepth_value> | wc -l

 

Method 6: Counting hidden files with a tree

You may wish to count hidden files on your system in various instances.

Hidden files are not printed in the terminal output by default when using the “tree“, “find“, or “ls” commands.

To count hidden files with the tree, run “tree” and add the “-a” option for “all,” followed by the directory to be inspected.

tree -a <directory>

 

Method 7: Counting files using GUI

You may find it simpler to count files in directories if you use a desktop interface such as KDE.

KDE Dolphin File Manager

Navigate to what you wish to investigate in the Dolphin File Manager.

Right-click on the folder and select “Properties.”

 

The “Properties” box will appear, displaying the number of files and subdirectories in the specified directory.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads