5 Ways to Count Files in a Directory on Linux


Key Takeaways

  • The “ls” command on Linux lists files and directories. Piping it to “wc -l” counts the total files and directories, including hidden ones.
  • The “tree” command displays the entire directory structure recursively and summarizes file counts at the end. Adding the “-a” option displays hidden files as well.
  • The “find” command with options like -type, -mindepth, and -maxdepth can recursively count files and directories. Use “-type f” to count only files, and “-type d” to count only directories.


Working with operating systems like Linux, managing files is one of the fundamental tasks. If you are a Linux admin, imagine the file count you have to deal with. And if your files consume a lot of disk space, then it will be a tough nut to crack. Let’s discuss some ways to count these files, including the pesky hidden files.


Why Do We Need to Count Files on Linux?

If you’re a Linux administrator or just starting with Linux commands, you’ll eventually need to count files in a Linux directory. You may need to free up space on your system, plan to back up your system files and estimate the amount of data to be backed up, or simply want to keep your files organized. It’s a task that every Linux user needs to know how to do.

Assume while working, your system runs out of inodes. Suddenly, you face the dreaded “No space left on device” error, and you can’t create any new files or directories. That’s when it’s time to roll up your sleeves and manage the disk-consuming files and directories by counting them. Luckily, there exist some Linux commands to get this done.

Counting Files and Directories Including Hidden Files Using the wc Command

On Linux, the ls command can list all files and directories. This command when piped (|) with the wc command results in the total count of files and directories including the hidden one.

Before we start moving to counting files, let’s list the actual files in the ~/HTG directory using the ls command. Here we have a total of ten files including four sub-directories inside the ~/HTG main directory. There is one hidden file, along with one text file, and four test files.

All files and directories listed

To list files, it is recommended to use ls -A instead of ls -a. The ls command with -a option includes the special entries—(.) current directory and (..) parent directory. This results in an overall increase in file count.

To count the number of files and directories first navigate to the directory and execute the following command:

ls | wc -l

ls command list all directories excluding hidden ones

First, the ls command lists all files and directories in the ~/HTG directory without any formatting options. The output of ls consists of a simple list of file and directory names, with each name on a separate line.

To count the files listed by the ls command, we’re piping (|) its output to the wc -l command. The wc command counts these listed files and outputs the file count number. Remember this command output nine because it doesn’t include one hidden file present in the ~/HTG directory.

We have different options to use with the ls command. For example, ls -l list files and directories in the current directory using the long listing format (-l). It provides detailed information about each file, including permissions, owner, size, and modification date.

ls -l | wc -l

ls command used with long listing format

This command does not include the hidden files, but it includes an entry for the directory itself (represented by .) in the long listing format—which in this case is ~/HTG directory. As a result, it increases the overall count by one.

If you want a consistent count regardless of listing format, you can use the -A option with ls to exclude the special entries (.) current directory and (..) parent directory, when counting:

ls -A | wc -l

ls command list all files including hidden

This should provide the same count as the ls -l command. The output is 10 because this command also includes one hidden file.

If you want a command that includes hidden files, the parent directory (..), and the current directory (.) in the total count, use the ls -a option with wc command.

ls -a | wc -l

ls list comand with all listing option

Related: How to Use the wc Command in Linux

Counting the Number of Files and Directories Using the tree Command

To count the number of files and directories in all the subdirectories, you can use the tree command. The tree command prints the entire directory structure recursively and displays a summary at the end of the output.

<directory> tree

draw tree of current directory

You may find the tree command missing error, to resolve it install the tree command using:

For Ubuntu / Debian hosts: sudo apt install tree For CentOS / RHEL hosts: sudo yum install tree

By default, the tree command doesn’t print the hidden files. To display the hidden file use the -a option with the tree command:

tree -a

tree command with hidden option

Now all files are listed and counted including the hidden ones.

Related: How to Traverse a Directory Tree on Linux

Recursively Counting Files Using the find Command

The find command on Linux with its various options, like -type, -mindepth, and -maxdepth, can help to perform recursive counting easily. It determines the total count of items in a directory structure without manually navigating into each subdirectory and counting them one by one.

find <directory> <options> | wc -l

The find command makes the search recursive because by default it counts through every single subdirectory. It does not stop its search at first depth.

To count all entries (both files and folders) inside a directory use:

find HTG | wc -l

command to list all files in current directory

This command also counts the current directory which in this case is ~/HTG—overall increases the search result by one.

The find command without any specific conditions includes both regular and hidden files in its search by default.

To count only files use:

find HTG -type f | wc -l

command to count all files

To count only directories including the current directory, in this case ~/HTG, use:

find HTG -type d | wc -l

command to list and count all directories

The find command can also search for files that match specific patterns (“.txt”, “.pdf”, “.sh”):

find . -type f -name "*.txt" | wc -l

command to search for text files

This command outputs the number of files in the current directory that end with the “.txt” extension.

When you’re counting files in a folder and its subfolders, sometimes you won’t be allowed to look into all the subfolders, so your system might show an error like “permission denied“.

denied folder permission

You can use a technique called “output redirection” to redirect these error messages.

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

deviate permission denied error to null

We can also modify the search result using the “mindepth” and “maxdepth” options.

find HTG -mindepth 1 -maxdepth 1 | wc -l

options to search from inside the current directory

In this case, -mindepth 1 means that the search starts at a minimum depth of 1, skipping the root directory itself and beginning the search in its immediate subdirectories.

The -maxdepth 1 option conducts the search at a maximum depth of 1. It doesn’t explore subdirectories of the immediate subdirectories.

Related: How to Use the find Command in Linux

Count Files in a Linux Directory with a Bash Script

On Linux, Bash scripts can automate repetitive tasks. The same is the case here, we can write a bash script and define a directory to count the files present in it.

The given bash scripts use the find command piped with wc to count all files in the ~/HTG directory. This script when executed outputs 11 because it also counts the current directory, increasing the result by one.

 #!/bin/bash
dir="HTG"
count=$(find "$dir"; | wc -l)
echo "There are $count files in the $dir directory."
Executed Bashscript output 11 files

As explained in the find command, to exclude the special directories from the final count, add -mindepth 1 option to the find command.

Counting Files Using a GUI

Counting files on Linux using the desktop interface like KDE or GNOME is super easy! It’s just like counting files on Windows.

Open the directory or folder to check by going to the activity menu or using the “Home” directory.

working directory opened using GUI

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

working directory properties opened

A new window pops up showing the total number of items in the folder.

display file count of current directory

Remember, this method only displays the total number of all items (such as “text files”, “.sh files”, and “directories”) and does not count individual files. At least, Ubuntu’s default file browser Nautilus doesn’t, but others, like Dolphin Browser, do show you more data.

Check the Necessary Permissions for File Counting

While executing all these commands, make sure you have the necessary permissions granted to the working folder. Type “sudo” before a command if you encounter a permission error. Also, check the syntax of the command in case you encounter any errors. To get the most out of these commands, refer to their respective help guides by using the “man” command.



Source link

Previous articleLamborghini Lanzador Concept — what it’s like to drive an extreme EV vision of the future
Next articleGoPro Hero 12 Black vs GoPro Hero 11 Black: What’s new?