In linux, we can ls command to list all files and directories in a directory. However, how to find how many files in a directory? In this tutorial, we will tell you how to do.
Find the number of files in the current directory
We can use command below to get the number:
ls -l|grep "^-"| wc -l
Run this code, we may see:
However, we should notice: this command only can count the number of files in this current directory, but do not count the number of files in subdirectories and the number of directories in current directory.
How to get the number of files in the current directory and subdirectory?
We can use command below:
ls -lR|grep "^-"| wc -l
Run this code, may see:
Here we add argument R in ls command.
How to get the number of directories in the current directory?
We can use command below:
ls -l|grep "^d"| wc -l
Meanwhile, we also can add R argument to get the number of directories in subdirectories.
ls -lR|grep "^d"| wc -l