In this beginner-friendly tutorial, we’re going to show you how to count files in a directory on Linux.
For this tutorial, you’ll need access to the Terminal (open it with CTRL + ALT + T) or SSH access to a server. This tutorial will only include instructions for the CLI. If you want to count the files in a directory via the GUI (Graphical User Interface) just right-click on the directory and click on Properties. You’ll get a window with stats about that directory, including the number of files.
Count files in a directory with the wc command
The easiest and most widely used Linux command to count files in a directory is:
ls -1 | wc -l
A brief explanation of this command:
- “ls” is used to list the files in the current directory. It’s short for “list”
- -1 is so it doesn’t count the first line you get from the “ls -l” command, which is just a header row with the sum of all the filesizes
- | is used to pipe the output of the previous command to the command after |
- wc is a command used to find the “word count”, count the number of lines, and other counting purposes
An example use of this command:
root@x77:/etc/opt/random# touch {1..26}notes.txt root@x77:/etc/opt/random# ls 10notes.txt 15notes.txt 1notes.txt 24notes.txt 4notes.txt 9notes.txt 11notes.txt 16notes.txt 20notes.txt 25notes.txt 5notes.txt 12notes.txt 17notes.txt 21notes.txt 26notes.txt 6notes.txt 13notes.txt 18notes.txt 22notes.txt 2notes.txt 7notes.txt 14notes.txt 19notes.txt 23notes.txt 3notes.txt 8notes.txt root@x77:/etc/opt/random# ls -1 | wc -l 26
We first created 26 .txt files in the previously empty /etc/opt/random directory. Then we just used the plain “ls” command to output all the filenames in the current directory, and finally, we run the “ls -1 | wc -l” to count the number of files in the directory, which in this case returned a result of “26”.
Count files in a directory with the egrep command
A similar command that outputs a similar result to the previous wc command is the egrep command. This command will output the number of files in the current directory. The command is:
ls -l . | egrep -c '^-'
and returns the number of files in the directory where you run this command. It works very similarly to the previously explained wc command.
Count files in a directory recursively with the find command
If you want to count the number of files in a certain directory, AND all the other files within the directories that are in that parent directory, you can use the find command:
find directoryName -type f | wc -l
So, for example, if:
- /etc/opt/random had 26 files
- /etc/opt/random/new had 10 files
- /etc/opt/random/old had 5 files
Running this command:
find /etc/opt/random -type f | wc -l
Would return a result of “41”. Which is 26+10+5.
“-type f” is used to only count files.
If you’d like to count the files in the current directory (the directory you’re in), just replace the directoryName with a dot, like so:
find . -type f | wc -l
Count files and directories using the tree command
The tree command might not be installed by default on your distro. You can install it by running
- “apt install tree” for Ubuntu/Debian
- “dnf install tree” for CentOS/Fedora
The basic syntax of the command is
tree directoryName
It returns a list of all the files and directories in a directory with their hierarchy.
So if you run the tree command for the random directory from our examples:
tree /etc/opt/random
You’d get this result:
/etc/opt/random ├── 10notes.txt ├── 11notes.txt ├── 12notes.txt ├── 13notes.txt ├── 14notes.txt ├── 15notes.txt ├── 16notes.txt ├── 17notes.txt ├── 18notes.txt ├── 19notes.txt ├── 1notes.txt ├── 20notes.txt ├── 21notes.txt ├── 22notes.txt ├── 23notes.txt ├── 24notes.txt ├── 25notes.txt ├── 26notes.txt ├── 2notes.txt ├── 3notes.txt ├── 4notes.txt ├── 5notes.txt ├── 6notes.txt ├── 7notes.txt ├── 8notes.txt ├── 9notes.txt ├── new │ ├── 10notes2.txt │ ├── 1notes2.txt │ ├── 2notes2.txt │ ├── 3notes2.txt │ ├── 4notes2.txt │ ├── 5notes2.txt │ ├── 6notes2.txt │ ├── 7notes2.txt │ ├── 8notes2.txt │ └── 9notes2.txt └── old ├── 1notes3.txt ├── 2notes3.txt ├── 3notes3.txt ├── 4notes3.txt └── 5notes3.txt 2 directories, 41 files
The last line of the output of this command will be the number of directories and files in that directory.
The tree command has more options you can use, like:
- tree -h to display the size
- tree -a to display and count all the files, including the hidden ones
- tree -d to display and count the directories only.
- tree –help to get more explanations and help with the tree command
Count a large number of files in a directory
If you had a directory with thousands of files in it, like 100000+ files, it might take longer to execute the standard “ls -1 | wc -l” command. To make it quicker (and make the command do less work), you need to use:
ls -f | wc -l
By default, ls sorts the directories and files. This can take a while if you have thousands of files. If you use the -f option, you’ll disable sorting. This command will also count hidden files.
Count and display the number of files in all directories
If you’d want to count and display the number of files in all (sub)directories within the current directory, use this command:
du -a | sed '/.*\.\/.*\/.*/!d' | cut -d/ -f2 | sort | uniq -c
If you run this command in the /etc/opt/random folder from our previous examples, you’d get this result:
10 new 5 old
Conclusion on finding and counting files in a directory on Linux
These were just a few examples of some of the commands you can use on Linux (any distro) to count files in a directory. There are more commands you can use, and there are more examples and use cases, but these will cover most of your needs, especially if you are a beginner.
If you have any other suggestions or examples of commands (or even bash scripts) to use for counting files, write them in the comments below.