If you are a Linux admin, imagine the file count you have to deal with.

Let’s discuss some ways to count these files, including the peskyhidden files.

Why Do We Need to Count Files on Linux?

Four hard drivers sticking out of a rack-mount server that’s being used as a NAS.

It’s a task that every Linux user needs to know how to do.

Assume while working, your systemruns out of inodes.

That’s when it’s time to roll up your sleeves and manage thedisk-consuming files and directoriesby counting them.

A PC running Linux.

Hannah Stryker / How-To Geek

Luckily, there exist some Linux commands to get this done.

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

current directory and (..) parent directory.

This results in an overall increase in file count.

To count the files listed by thelscommand, we’re piping (|) its output to thewc -lcommand.

ls command list all directories excluding hidden ones

Thewc commandcounts these listed files and outputs the file count number.

Remember this command output nine because it doesnt include one hidden file present in the ~/HTG directory.

We have different options to use with thelscommand.

ls command used with long listing format

It provides detailed information about each file, including permissions, owner, size, and modification date.

in the long listing formatwhich in this case is ~/HTG directory.

As a result, it increases the overall count by one.

ls command list all files including hidden

The output is 10 because this command also includes one hidden file.

in the total count, use thels -aoption withwccommand.

The tree command prints the entire directory structure recursively and displays a summary at the end of the output.

ls list comand with all listing option

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.

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

draw tree of current directory

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

The-maxdepth 1option conducts the search at a maximum depth of 1.

It doesn’t explore subdirectories of the immediate subdirectories.

tree command with hidden option

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.

Counting Files Using a GUI

Counting files on Linux using the desktop interface likeKDEorGNOMEis super easy!

command to list all files in current directory

It’s just like counting files on Windows.

Right-hit the folder and choose “Properties” option.

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

command to count all files

throw in “sudo” before a command if you stumble on a permission error.

Also, check the syntax of the command in case you encounter any errors.

command to list and count all directories

command to search for text files

denied folder permission

deviate permission denied error to null

options to search from inside the current directory

Executed Bashscript output 11 files

working directory opened using GUI

working directory properties opened

display file count of current directory