Count Number of Files and Directories in Directory on Linux

Count Number of Files and Directories in Directory on Linux

Count number of files in directory and subdirectory Linux. In this tutorial guide, you will learn how to count number of files in directory and subdirectory linux. And as well as how to count hidden files and directories in Linux system.

Whenever you are probably monitoring disk space on your system all the time. And as well as, you may want to know How many files and directories are in the given directory, or in many different directories or subdirecotories.

So, this tutorial guide will help you on how you can easily count files and directories in a directory on Linux using lsfind and tree commands.

How to Count Files in Directory in Linux

Different ways to count number of files and directories in directory, subdirectory and hidden directory linux:

  • Count Files using wc
  • Count Files Recursively using find
  • Count Files using tree
  • Count Hidden Files

Count Files using wc

Now, you will learn the easiest way to count files in a directory on Linux using the “ls” command and pipe it with the “wc -l” command, As shown below:

ls | wc -l

Note that, The “wc” command is used on Linux in order to print the bytes, characters or newlines count.

Now, i will run the “ls” command on the “/html” directory and pipe it with the “wc” command, as shown below:

ls /html | wc -l

The output will be as shown below:

500

Count Files Recursively using find

Count files recursively on Linux using the “find” command and pipe it with the “wc” command. As shown below:

find <directory> -type f | wc -l

For example, if you want to recursively count files in the “/html” directory, you would write the following query:

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

Count Files using tree

If you want to count files and directories in a directory. So, you can use “tree” command and to specify the name of the directory to be inspected. As shown below:

tree <directory>

This is very important thinga 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

Count Hidden Files

If you want to count hidden files and directories in a directory. So, you can use “tree” command with -a parameter. As shown below:

tree -a <directory>

Conclusion

Count files and direcotries in linux tutorial guide, you have learn how to count files and directories in directory using the lsfind and tree commands.

Recommended Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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