How to count number of files and folder in a directory

I have posted 5+ commands and examples to count files and directory in linux.

The below command is count, how many files are in current directory.

ls -l | grep ^- | wc -l

Output:Totally 2 files are available in current directory

[email protected] [/usr/local/nagios]# ls -l | grep ^- | wc -l
2

The below command is count, how many folder/directory are in current directory.

ls -l | grep ^d | wc -l

Output:Totally 9 folders are available in current directory

[email protected] [/usr/local/nagios]# ls -l | grep ^d | wc -l
9

The below command is count, how many symbolic links are in current directory.

ls -l | grep ^l | wc -l

Output:There is no symbolic links available in current directory

[email protected] [/usr/local/nagios]# ls -l | grep ^l | wc -l
0

The below command is count, how many files and directory are in current directory.

ls | wc -l
or
ls -l | wc -l

Output:This will tell you the number of files & folders in a directory

[email protected] [/usr/local/nagios]# ls -1 | wc -l
11

The below command is count files and folders recursively in current directory

find -type f | wc -l

Output:This will tell you the number of files & folders count recursively in a directory

[email protected] [/usr/local/nagios]# find -type f | wc -l
755

The below command is count only folders recursively in current directory

find -type d | wc -l

Output:This will tell you the number folders count recursively in a directory

[email protected] [/usr/local/nagios]# find -type d | wc -l
31

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...