Count Number of Files and Directories in Directory on Linux

To count the number of files and directories in a directory on Linux, you can use the command “ls -l | wc -l”. This command will list all the files and directories in the directory and then count the number of lines in the output.

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

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?