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

count-number-files-linux-featured

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:

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

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

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

5 comments

  1. You can also install the tree command if you want to count the number of files or folders in a tree. Use ‘sudo apt install tree’ to install it.

  2. Regarding the command line methods …

    1. This is not a correct count as ‘ls -l’ also prints a ‘total ‘. The correct count is therefore the count minus one.

    2. It reports only the number of files because that is what you have told it to do ‘… -type f …’.
    If you want it to list both directories and files (and everything else) just do ‘find | wc -l’, that will recursively ‘find’ all filesystem entities from the folder you are working in and down in the tree.
    (If you want to find only directories ‘find -type d’)

    Cheers

  3. “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.”

    The console command is only listing files due to the “-type f” flag on find, this means find files of type “regular file” (i.e: no links, directories, sockets etc).

  4. Also, the command “ls -l'” does not list hidden files. You need the -a flag. This will affect the count. Your GUI file manager may or may not be set to show them.

  5. Thank you all for the clarifications, me and the prompt are not exactly best friends :) and honestly I didn’t check the commands in great detail, I thought it was more of a bug in either the GUI, or the shell, or both. And the gazillion of 1 and 2 letter options each command has is simply more than what my brain can hold. :)

Comments are closed.

Sponsored Stories