Knowing how to efficiently count files and directories in Linux is a valuable skill for system administrators and anyone who works with large amounts of data. In this guide, I'll share how I use simple command line tools, such as find
, wc
, and ls
, to quickly find the exact number of files and folders in Linux and Unix operating systems.
Let us get started!
Table of Contents
Count Files in a Directory using find
Command
To count all the files in a given path, including all of its sub-directories, you can use the find
command combined with wc -l
in your Linux terminal. Here's how you can do it:
1. Open your terminal.
2. Use the following command, replacing /path/to/your/directory
with the actual path of your directory:
$ find /path/to/your/directory -type f | wc -l
Let us breakdown the above command and see what each part of the command does:
find /path/to/your/directory
starts the search within the specified directory.-type f
tellsfind
to look for files only (not directories).- The
|
(pipe) passes the output of thefind
command to the next command. wc -l
counts the number of lines in the input it receives, which, due to the use offind
, corresponds to the number of files.
This will output the total number of files in the specified directory and all of its sub-directories.
Example:
To count the number of files in Downloads Directory, the command would be:
$ find ~/Downloads -type f | wc -l 5968
As you can see, there are 5968 files in the Downloads directory.
Count Directories using find
Command
In the previous example, we counted the number of files in the given location. What about the directories? Can we use the find command to sub-directories in a specific directory?
Yes, we can use a command line to find the number of sub-directories within a given directory in Linux. You'll use the find
command again, but this time you'll look for directories (-type d
) instead of files.
$ find /path/to/the/directory -type d | wc -l
Just replace /path/to/the/directory
with the actual path. Here's the breakdown of the command:
find /path/to/the/directory
starts the search within the specified directory.-type d
tellsfind
to look for directories only.- The
|
(pipe) passes the output of thefind
command to the next command. wc -l
counts the number of lines in the input it receives, which corresponds to the number of directories found byfind
.
This command counts all directories including the root directory of the search.
Example:
$ find ~/Downloads -type d | wc -l 563
As you can see, there 563 sub-directories in the ~/Downloads
directory including itself.
If you want to exclude the root directory from the count, simply subtract 1 from the result.
Count Files and Directories with find
Command using the maxdepth
and mindepth
Options
When counting files and directories with the find
command in Linux, using the maxdepth
and mindepth
options effectively can help you target your search precisely and avoid counting unnecessary items.
Here's how you can use these options for counting purposes.
Counting Files within a Specific Depth Range
To count files within a specific range of directory depths, you can specify both mindepth
and maxdepth
. This is useful when you want to exclude top-level files or dive into specific sub-directory levels.
Example: Count files in the second level of sub-directories only:
$ find /path/to/directory -mindepth 2 -maxdepth 2 -type f | wc -l
This command will count files that are exactly two directories deep from the starting point.
Counting Directories at a Specific Depth
Similarly, to count directories at a specific depth, you can adjust the mindepth
and maxdepth
options accordingly.
Example: Count directories that are exactly three levels deep:
$ find /path/to/directory -mindepth 3 -maxdepth 3 -type d | wc -l
Excluding Top-Level Directory from the Count
If you're counting items but want to exclude the top-level directory itself or any items in the top-level directory, you can use the following example command.
Example: Count all files excluding the top-level directory:
$ find /path/to/directory -mindepth 1 -type f | wc -l
Example: Count sub-directories excluding the top-level:
$ find /path/to/directory -mindepth 1 -type d | wc -l
Counting All Files or Directories Up to a Certain Depth
If you want to include everything up to a certain depth, you would use maxdepth
without specifying mindepth
.
Example: Count all files up to and including the second level of sub directories:
$ find /path/to/directory -maxdepth 2 -type f | wc -l
Tips for Effective Use
- Be clear about your target: Understand whether you're interested in files, directories, or both, and adjust the
-type
option (f
for files,d
for directories) accordingly. - Consider performance: Searching very deep directory structures can be time-consuming. If you're only interested in a specific depth range, setting
maxdepth
andmindepth
appropriately can speed up your search. - Verify your depth assumptions: Before running a large count operation, test your command with a small subset of your target directory to ensure you're counting at the correct depth.
You can count files and folders in Linux more accurately and quickly by adjusting how deep you want to search using "mindepth
" and "maxdepth
" according to what you need.
Count Directories and its Sub-directories using ls
Command
The ls, grep, and wc combination is another method to count the number of directories within a specific directory, including all its sub-directories.
Navigate to the directory in which you want to count the number sub-directories:
$ cd MyFiles/
And execute the following command:
$ ls -lR | grep ^d | wc -l 488
As you see in the output above, the total number of sub-directories in the ~/MyFiles
are 488.
Here's a brief explanation of how this command works:
- ls -lR: This command lists all files and directories recursively in the current directory and its sub-directories, showing detailed information about each file and directory.
grep ^d
: This command filters the output of thels -lR
command, selecting only lines that start with "d", which indicates directories.wc -l
: This command counts the number of lines in the output generated by thegrep ^d
command.
So, altogether, the command lists all directories and sub-directories in the current directory and its sub-directories, then counts the number of directories and outputs that count. Essentially, it tells you how many directories exist within the current directory and its sub-directories.
It's a handy method, especially when you're already working within a specific directory and want a quick directory count.
Why the find
and ls
Commands Show Different Count when Counting Directories?
The commands ls -lR | grep ^d | wc -l
and find /path/to/directory -type d | wc -l
both count directories, but they do so in different ways, which may lead to different results.
Here's why the count differs:
1. Hidden Directories:
find
command: By default,find
includes hidden directories (those starting with a.
) in its search and count.ls -lR
command: This combination, without additional options, does not list hidden directories unless explicitly told to do so (using the-a
option withls
).
2. Root Directory Count:
find
command: The count obtained fromfind /path/to/directory -type d | wc -l
includes the root directory specified in its total count.ls -lR | grep ^d | wc -l
command: This method typically counts only the sub-directories and not the root directory from which the command is run, depending on how the output is interpreted bygrep
.
Conclusion
Whether you're cleaning up an external hard drive, or just curious about the contents of a folder, being able to quickly count the number of files and directories within a specific location is an essential skill.
By utilizing the appropriate commands like find
, ls
, and wc
, you can efficiently obtain the exact number of files and directories within a specific location or even your entire Linux system.
Related Read: