Skip to content

How to count files in a directory in Linux?

  • by
  • 3 min read

Photo: Jivacore/Shutterstock.com

Linux is long past the days when you needed to know hundreds of terminal commands to be able to do anything with the operating system. These terminal commands are still as quick and helpful as they used to be, meaning they’re still relevant. 

In this article, we’re looking at how you can count files in a directory in Linux using both the terminal commands and the GUI.

Also read: How to fix Curl command not found on Linux?


Using terminal commands

If you’re interested, using terminal commands can provide a few quick ways to count your files. 

Counting using wc

By far the easiest way to count files in a directory is using the ls command piped with the wc -l command. Here’s how.

ls [directory name] | wc -l

Counting recursively using find

As the title suggests, you can count files recursively using the find command. Here’s how.

find [directory name] -type f | wc -l

Counting using tree

Another terminal command that you can use to count files in a directory in Linux is the tree command. This will also show you the number of directories.

tree [directory name]

In case you want to include hidden files in the count as well, you can use the -a flag. 

tree -a [directory name]

Also read: How to make a file executable in Linux?


Using the GUI

Modern Linux systems have rather well-designed user interfaces. If you’d rather use the GUI, here’s how.

KDE Dolphin File Manager

When using the KDE Dolphin File Manager, all you have to do is open the file manager, navigate to the directory you want to count the files in and right-click an empty space. Select Properties from the list of options that appears. You should see the number of files and directories under the General tab. 


GNOME File Manager

GNOME might look different, but follows a very similar approach to checking the number of files in a directory. Simply navigate to the directory you want to count the files in and right-click an empty space. Select Properties from the list of options that appears.

Under the Basic tab, you should see the total number of items in a directory. Note that this includes any sub-directories as the GNOME file manager doesn’t show files and sub-directories separately.

Also read: What is DynamicLinkMediaServer taking up memory?

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>