In Linux, you can count the number of files and directories in a directory using the ls command in combination with other commands such as wc and grep.
The easiest way to count number of files and subdirectories in a directory using a tree command, which is best known for displaying files and directories in tree-like form.
Counting files and directories on a web hosting server involves using command-line tools, just as you would on a local Linux machine.
You may have access to the server through SSH (Secure Shell) or a web-based terminal provided by your hosting provider. Here’s a basic guide using SSH

Table of Contents
Count files & directories for inode usage
If you have the tree command installed, you can use it to count files and directories based on inode usage.
The tree command provides a visual representation of the directory tree, and with the -i option, it shows inode numbers for each file and directory.
You can then use grep and wc to count the number of files and directories
Although you can always enable quotas to restrict disk space and inode usage to avoid user abuse, this command may be useful anyway. By default, the current working directory is assumed if no arguments are given
tree -iLf 1

Count directories
You can count directories using below command
tree -i -f -F | grep "/$" | wc -l

Count files
You can count files using below command
tree -i -f -F | grep -v "/$" | wc -l

tree -i -f -F: This command generates a directory tree with inode numbers (-i), displaying the full path (-f), and appending a ‘/’ to directory names (-F).- For counting directories:
grep "/$": This filters the output to include only lines ending with a ‘/’. Directories in the tree are marked with a trailing ‘/’.wc -l: This counts the number of lines, giving you the count of directories.
- For counting files:
grep -v "/$": This filters the output to exclude lines ending with a ‘/’. This includes regular files since they don’t have a trailing ‘/’.wc -l: This counts the number of lines, providing the count of files.
These commands give you separate counts for directories and files based on their inode usage in the specified directory and its subdirectories. Adjust the starting directory as needed.
Count files & directories for var/log
If you want to view the same information for /var/log, do:
tree -iLf 1 /var/log

Type the command below to view information about files and subdirectories in the directory ISOs
tree -iLf 1 ISOs

Explaining tree options used in the command above:
-i– its a graphical option that enables tree to print out indentation lines-L– specifies the level of depth of the directory tree to be displayed, which in the case above is 1-f– makes tree print the full path prefix for every file
As you can view from the image above, after listing all the files and subdirectories, tree shows you the total number of directories and files in the directory you specified.
To count files and directories in the /var/log directory based on inode usage using the tree command, you can use the following commands:
Count directories
You can count directories for /var/log using below command.
tree -i -f -F /var/log | grep "/$" | wc -l

Count files
You can count files for /var/log using below command.
tree -i -f -F /var/log | grep -v "/$" | wc -l

tree -i -f -F /var/log: This command generates a directory tree with inode numbers (-i), displaying the full path (-f), and appending a ‘/’ to directory names (-F) for the/var/logdirectory.- For counting directories:
grep "/$": This filters the output to include only lines ending with a ‘/’. Directories in the tree are marked with a trailing ‘/’.wc -l: This counts the number of lines, giving you the count of directories.
- For counting files:
grep -v "/$": This filters the output to exclude lines ending with a ‘/’. This includes regular files since they don’t have a trailing ‘/’.wc -l: This counts the number of lines, providing the count of files.
These commands will give you separate counts for directories and files based on their inode usage in the /var/log directory. Adjust the starting directory as needed.
You can refer to the tree man page to discover more useful options, some configuration files and environment variables to better understand how it works.
We covered an important tip that can help you use the tree utility in a different way as compared to its traditional use, for displaying files and directories in a tree-like form.