Count the Number of Files in a Directory and Subdirectories Linux

To count the number of files in a directory and its subdirectories, you can use ls, find, and tree commands with its options on Linux command line.

Here are some approaches on how to count number of all files in a directory and its subdirectories using Linux command line:

  • Approach 1: Count the Number of Files Using ls and wc Commands in Linux
  • Approach 2: Count the Number of Files Using find and wc Commands in Linux
  • Approach 3: Count the Number of Files Using the tree Command in Linux

Approach 1: Count the Number of Files Using ls and wc Commands in Linux

To count the number of files in a directory and subdirectories, you can type “ls -l | wc -l” command on the terminal window:

ls | wc -l

In this, ls command will list the files from the directory, and wc -l command will count how many lines are there and calculate the count of number of files in a directory and subdirectories.

Let us understand this with an example, suppose you have an HTML directory and it also has some subdirectories, now, to count the number of files in the directory and subdirectories in Linux terminal, you can use this command:

ls /html | wc -l

The output will be as shown below on Linux command line:

500

Approach 2: Count the Number of Files Using find and wc Commands in Linux

You can type “find <directory> -type f | wc -l” command on the linux command line, To count the number of files in a directory and subdirectories:

find <directory> -type f | wc -l

The “find <directory> -type f” will find for files in directories and subdirectories, and wc -l will count how many files there are.

For example, Suppose you have “/html” directory and its subdirectories, you can type this command on terminal window:

find /html -type f | wc -l

If you are finding to some error messages with the above command, so you can use the following command on it to count files recursively in direcotry:

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

Approach 3: Count the Number of Files Using the tree and wc Command in Linux

When you use tree on Linux terminal, the tree command shows the subdirectories and files inside a directory in a tree format, with this you will use wc -l to count the number of files in a directory and sub-directories, you can use:

tree -f . | wc -l

This is a very important thing about the “tree” command, it is not installed on all hosts by default.

If you are having a “tree : command not found” or “tree : no such file or directory”, you will have to install it using sudo privileges on your linux system. By executing the following commands:

sudo apt-get install tree  // for Ubunbu/Debian hosts

sudo yum install tree     //  for CentOS/RHEL hosts

To count the number of hidden files in a directory and its subdirectories linux terminal, you can use tree | wc -l with -a option in tree command, you can use:

tree -a . | wc -l

Here is the video guide on counting all files in the directory and subdirectory linux:

Conclusion

That’s it; you have learned how to count number of files in directory and subdirectory on linux ubuntu command line using the lsfind and tree commands.

Recommended Posts

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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