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?
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.
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.
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.
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.
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.
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.
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.
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.
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!
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.
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.