Summary

Working withoperating systemslike 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 peskyhidden files.

Why Do We Need to Count Files on Linux?

If you’re a Linux administrator or juststarting with Linux commands, you’ll eventually need to count files in a Linux directory. You may need tofree up space on your system, plan toback up your systemfiles 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 systemruns out of inodes. Suddenly, you face the dreaded “No space left on device” error, and you may’t create any new files or directories. That’s when it’s time to roll up your sleeves and manage thedisk-consuming files and directoriesby counting them. Luckily, there exist some Linux commands to get this done.

All files and directories listed

Counting Files and Directories Including Hidden Files Using the wc Command

OnLinux, thelscommandcan list all files and directories. This command whenpiped(|) with thewccommand 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 thelscommand. 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.

draw tree of current directory

To list files, it is recommended to usels -Ainstead ofls -a. Thelscommand with-aoption 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:

tree command with hidden option

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

To count the files listed by thelscommand, we’re piping (|) its output to thewc -lcommand. Thewc commandcounts 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.

denied folder permission

We have different options to use with thelscommand. For example,ls -llist 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.

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.

working directory opened using GUI

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

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

working directory properties opened

If you want a command that includes hidden files, the parent directory (..), and the current directory (.) in the total count, use thels -aoption withwccommand.

Related:How to Use the wc Command in Linux

display file count of current directory

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 thetreecommand. The tree command prints the entire directory structure recursively and displays a summary at the end of the output.

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-aoption with the tree command:

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

Thefind commandon 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.

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:

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:

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

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

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

you may use a technique called “output redirection” to redirect these error messages.

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

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

The-maxdepth 1option 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 scriptscan 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.

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

Counting Files Using a GUI

Counting files on Linux using the desktop interface likeKDEorGNOMEis 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.

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

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

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.