Find the Number of Files in a Directory Using ls Command in Linux – Linux Tutorial

By | July 19, 2022

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:

Find the Number of Files in a Directory Using ls Command in Linux - Linux Tutorial

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:

Find the Number of Files in a Directory Using ls Command in Linux example 2- Linux Tutorial

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

Leave a Reply