How to Count the Number of Files in a Directory in Linux

When you have a folder full of files (and sub-folders) you might need to find out how many files are there altogether. Counting files and folders one by one is certainly not an option, especially when there are less gruesome ways to do it, such as the following.

Let’s start with the easiest way. Just open the directory in a file manager and look at the status bar.

linux-count-files-01-file-manager

If you don’t see a status bar, check “View -> Status bar” in the menu to see if the status bar is enabled.

The disadvantage of this method is that it counts a folder as one item but doesn’t count the number of files in it. Actually, the figure you are getting for how many files are in the directory is the sum of the number of folders and the number of separate files in this directory. If you want to view them separately (e.g. the number of files or the number of directories only), you need to select them, and then the status bar will show the number of the files/directories in the selection only.

In addition to showing the number of all files and folders in a directory, File Manager will allow you to do more. For instance, if you want to count only files or only folders, or only a part of the files/folder in a directory, just select them, and the status bar will show the number of files/folders in the selection. For example, if I wanted to see the number of the JPEG files only, I would select them. The result is shown in the status bar.

linux-count-files-02-selection

The status bar looks great if you don’t have a lot of files and folders, but if you do, there are better ways to count them. For instance, the below image is of a directory listing of one of my working folders.

linux-count-files-03-list

You see, it’s only directories with lots of files in each of them. The File Manager doesn’t show this, but if I select them and right click to open “Properties,” the Properties window tells me how many there are.

linux-count-files-03-properties

I guess the output varies from one file manager to another because I do recall seeing a different output (like a separate number for files and for folders), so what you get depends a lot on the file manager you are using.

If the simple ways of counting files and folders don’t work for you, or if you are a terminal person, the good news is there are numerous ways to get some data about your files and folders via the terminal. For instance, if you simply want a number, use this:

ls -1 | wc -l

linux-count-files-04a-simply-number

This command returns just the number of files/folders. To count files recursively, use this:

find DIR_NAME -type f | wc -l

linux-count-files-04b-recursively

I noticed that there is a difference in the numbers I get via the Properties window and via the console, and I assume this is because the console doesn’t count the directories or something.

There are other commands for directory listings such as list commands you can use to get a file number, among other things, but I am not going to discuss them. These commands come with many parameters, and the exact syntax varies from one Linux shell to the next. If you are interested in them, check the documentation of the shell you are using.

Image credit: Office Business Directory

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.