Home Linux System Administration How To Count Files in Directory on Linux

How To Count Files in Directory on Linux

by schkn

As a system administrator, you are probably monitoring the disk space on your system all the time.

When browsing directories on your server, you might have come across directories with a lot of files in them.

Sometimes, you may want to know how many files are sitting in a given directory, or in many different directories.

In other words, you want to count the number of files that are stored in a directory on your system.

In this tutorial, we are going to see how you can easily count files in a directory on Linux.

Count Files using wc

The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command.

$ ls | wc -l

The “wc” command is used on Linux in order to print the bytes, characters or newlines count. However, in this case, we are using this command to count the number of files in a directory.

As an example, let’s say that you want to count the number of files present in the “/etc” directory.

In order to achieve that, you would run the “ls” command on the “/etc” directory and pipe it with the “wc” command.

$ ls /etc | wc -l

count files in directory using wc

Congratulations, you successfully counted files in a directory on Linux!

Remark using wc command

An important command when using the “wc” command resides in the fact that it comes the number of newlines for a given command.

As a consequence, there is a big difference between those two commands

$ ls -l | wc -l


$ ls | wc -l


Even if we think that those two commands would give us the same output, it is not actually true.

When running “ls” with the “-l” option, you are also printing a line for the total disk allocation for all files in this directory.

ls command printing blocks

As a consequence, you are counting a line that should not be counted, incrementing the final result by one.

count files using wc

Count Files Recursively using find

In order to count files recursively on Linux, you have to use the “find” command and pipe it with the “wc” command in order to count the number of files.

$ find <directory> -type f | wc -l

As a reminder, the “find” command is used in order to search for files on your system.

When used with the “-f” option, you are targeting ony files.

By default, the “find” command does not stop at the first depth of the directory : it will explore every single subdirectory, making the file searching recursive.

For example, if you want to recursively count files in the “/etc” directory, you would write the following query :

$ find /etc -type f | wc -l


When recursively counting files in a directory, you might not be authorized to explore every single subentry, thus having permission denied errors in your console.

permission denied folder linux

In order for the error messages to be redirected, you can use “output redirection” and have messages redirected to “/dev/null”.

$ find /etc -type f 2> /dev/null | wc -l


Awesome, you recursively counted files in a directory on Linux!

Count Files using tree

An easy way of counting files and directories in a directory is to use the “tree” command and to specify the name of the directory to be inspected.

$ tree <directory>

3 directories, 3 files
count files on linux using tree

As you can see, the number of files and directories is available at the bottom of the tree command.

The “tree” command 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 system.

$ sudo apt-get install tree             (for Ubunbu/Debian hosts)

$ sudo yum install tree                 (for CentOS/RHEL hosts)

Counting hidden files with tree

In some cases, you may want to count hidden files on your system.

By default, whether you are using the “tree”, “find” or “ls” commands, hidden files won’t be printed in the terminal output.

In order to count hidden files using tree, you have to execute “tree” and append the “-a” option for “all”, followed by the directory to be analyzed.

$ tree -a <directory>

For example, if we count files and directories in your “/home” directory, you will be able to see that there is a difference because multiple hidden files are present.

$ tree /home/user

4321 directories, 27047 files

$ tree -a /home/user

9388 directories, 32633 files

Counting Files using Graphical User Interface

If you are using a Desktop interface like KDE or GNOME, you might have an easier time counting files in directories.

KDE Dolphin File Manager

A quick way of finding the number of files in a directory is to use the Dolphin File Manager.

Click on the bottom left corner of your user interface and click on the “Dolphin File Manager” entry.

dolphin file manager kde

When you are in the Dolphin File Manager, navigate to the folder that you want to explore.

Right-click on the folder and select the “Properties” option.

properties kde dolphin

The “Properties” window will open and you will be able to see the number of files and subdirectories located in the directory selected.

counting files on kde

Awesome, you counted the number of files in a directory on KDE!

GNOME Files Manager

If you are using GNOME as a Desktop environment, navigate to the “Activities” menu on the top-left corner of your Desktop, and search for “Files”.


When in the “File Explorer”, select the folder to be inspected, right-click on it and select the “Properties” option.

GNOME file properties

When in the “Properties” window, you will be presented with the number of “items” available in the folder selected.

Unfortunately, you won’t be presented with the actual number of “files”, but rather the number of “items” which can be found to be quite imprecise.

counting files on gnome

Awesome, you found the number of items available in a directory on Linux!


In this tutorial, you learnt how you can easily count files in a directory on Linux.

You have seen that you can do it using native commands such as the “wc” and “find” commands, but you can also install utilities in order to do it quicker.

Finally, you have seen how you can do it using user interfaces such as GNOME or KDE.

If you are interested in Linux System Administration, we have a complete section dedicated to it on the website, so make sure to check it out!

You may also like


Leigh Bowden November 21, 2020 - 7:41 am

No it doesn’t – Lubuntu.

$ sudo apt-get install tree
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package tree

Aastik Gaur January 10, 2022 - 5:19 am

sudo apt update

then try again

Umesh S G July 6, 2021 - 4:47 am

Very useful and nicely presented the information, Great for such a posts

Jose Laurentino July 24, 2021 - 11:42 am

Fantastic; thanks!

cgw November 17, 2021 - 3:43 pm

great, thank guidance dude

Karthika March 15, 2022 - 1:42 pm

How to count subdirectories in a directory and display the count

Bob Dole May 24, 2022 - 5:25 am

This was lovely and well laid-out. Thank you for taking the time to write it up!

Ludo August 3, 2022 - 2:44 pm

For the tree part, one can also pipe tree to tail the last line:
tree | tail -1

example of output:
1219 directories, 14543 files

Marcos September 7, 2022 - 11:49 am

Very good, I realize we can do this too:
ls /directory | grep .txt | wc -l
This will count all files that have .txt in their name


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.