Counting files in Linux is a common task many users need to perform. This helps you organize your files, understand disk usage, and manage them safely. You can count files using different CLI commands or your system’s default file manager (AKA) GUI method in Linux. Let’s look at some main ways of counting files in a Linux directory.
Key Takeaways
- To count files in Linux, you can pipe the “wc” command with “ls”. This will output the total count of all files in the directory, excluding hidden files.
- To count all files, including the hidden one, you can use the “find” command with the “-type f” flag and pipe it with the “wc” command.
- To get the file count information in the form of an illustration, try the Linux “tree” command.
1. How to Count Files in a Linux Directory
To count files in a Linux directory, you can use the wc command. First, use the ls command to list files and directories, then pipe it with the wc -l command to count the total files. This will count all files and directories. The output result also includes the hidden file count. For example, to count the total files inside the current directory, you can run ls | wc -l.
Similarly, you can use the tree command to recursively display the entire directory structure and summarize the file counts at the end. Adding the -a option to the tree command will also display hidden files.
The find command in Linux can also be used to count files. This command also helps you count files inside a specified directory. You can try multiple options with find commands like -type, -mindepth, and -maxdepth. Use the -type f option to count files and the -type d option to count only directories. For example, you can run the find . -type f | wc -l command to count files in the current directory.
1.1. Creating Some Demo Directories, Files, and Some Hidden Files
To help you with this tutorial, I will create some demo files, folders, and hidden files.
To create a demo directory, run:
mkdir test && cd test
Now that the test directory is created and we are inside it, let’s create a few text files using the touch command:
touch file{1..3}.txt
Here, I have created three text files with the .txt extension.
Now, create a new hidden file using this command:
touch .hidden1
In Linux, the files whose names start with the . (Dot) are hidden files.
To verify if all the files and folders are created, you can list them using the ls command:
ls -A
Here, the -A option of the ls command will list all the files in the test directory, including the hidden files.
Now, let’s create a new subdirectory inside the test directory. This will help us test how subdirectories work and how their files can be counted.
Here, I am going to create a new directory named subtest inside the primary parent test directory:
mkdir subtest && cd subtest
Before running the above command, ensure you are inside the leading parent test directory. Once the subdirectory is created, access it using the cd command.
Now, inside the subtest directory, create new text files:
touch file{4..5}.txt
Similarly, create a new hidden file using the touch command:
touch .hidden2
To confirm all the newly created files, you can run the ls -A command:
ls -A
This will give you a list of all files and folders inside the subtest directory, including the hidden file.
Now, we are all set to count these new files. The illustration below gives you a better perspective of the files and directories that I have created here.
Let’s proceed to file counting methods.
2. Count the Number of Files Using wc and ls Command
You can effectively combine Linux’s ls and wc commands to count files and directories. The wc command in Linux is a handy tool for counting various aspects of a file’s content.
The ls command is the workhorse for listing files and directories. It provides a detailed or basic file listing depending on the options used. By default, ls lists files in a multi-column format, making it difficult to count them accurately. However, you can pipe the ls command with wc to count files, folders, and directories.
Let’s check some examples of using wc commands with ls for file counting.
2.1. Counting all Files
First, ensure you are inside the directory where you need to count files. In our case, we have a test directory where we will count files.
Now run this command to count the files inside the test directory:
ls . | wc -l
This command uses piping (represented by the | symbol) to count the files in the current directory. Here’s a quick breakdown:
- ls .: This part lists the contents of the current directory (Dot refers to the current directory).
- wc -l: The -l option with the wc command counts the lines of the ls command output.
Since ls outputs a list with one line per file/directory, piping the output of ls to wc -l will give you lines (files) count in the directory listing.
Once executed, you will get this output on your terminal.
The output displays a total count of four. This is because the ls with the. (Dot) option excludes the hidden files while listing. Let’s see how to count hidden files, too.
2.2. Counting all Files, Including Hidden and Subdirectories
To include hidden files, use the -A flag with the ls command:
ls -A | wc -l
This command will display the total count of files within the current directory, including both visible and hidden files.
Here is a quick breakdown of the above command:
- ls -A: Lists all files in the current directory, including hidden files that typically start with a dot (.).
- |: This pipe symbol directs the output of ls -A (a list of filenames) to the next command.
- wc -l: This counts the number of lines received in the input.
Since ls -A shows each filename on a separate line, wc -l effectively counts the number of lines (filenames) as the number of files.
The output for the above command displays a total count of five.
This includes three text files, one subdirectory, and one hidden file.
Point to Ponder
While the -a flag with ls shows hidden files, it also counts the special entries . (current directory) and .. (parent directory). These aren’t actual files so that they can mess up your file count.
For a precise count, including hidden ones, use the -A flag instead. This flag means almost all, and it skips the . and .. entries, resulting in a more accurate file count.
2.3. Counting Files with Specific File Extension
To target specific file types, replace the -A flag in the ls -A | wc -l command with the file extensions. This lets you focus on files with a particular ending, like .jpg or .txt format files. This way, you can isolate and count only the files that need to be counted.
To count only text files (.txt) in the current directory, you can use this command:
ls *.txt | wc -l
The above command will give you the count of all text files in your current working directory. Here’s a quick breakdown of this command:
- ls: This is a command used to list files and directories.
- *.txt: This wildcard matches any filename ending with the extension .txt.
- |: This pipe symbol sends the output of the ls command (the list of files) as input to the wc command.
- wc -l: This is the wc command with the -l flag. The wc command counts lines, words, and characters in a file. The -l flag tells wc only to count lines.
The above command displays three in its output, as we have only three text files inside our current working directory.
While the ls and wc combo works for simple cases, it’s not ideal for complex directories. This is because it counts subdirectories as files, inflating the actual file count. In other words, this approach misses the mark since we only care about regular files, not folders within the directory.
3. Count the Number of Files Using tree Command
The tree command in Linux visualizes directory hierarchies in a tree-like structure. It is like a flashlight for your folders and files. It casts a light on their organization, showing how everything is connected in a branching structure, just like the branches of a tree.
The tree command is handy when working with numerous subfolders within folders. It unravels the nested complexity and makes everything clear. On top of that, the tree command even provides a handy count at the end, telling you exactly how many directories and files there are.
Before you can proceed with the file count using the tree command, first check if the tree command is available on your system or not:
tree --version
If it is missing, you can get it using this command:
sudo apt install tree
Once the tree command is ready, now navigate to the directory where you want to count files and run this command:
tree .
Note: The tree command operates recursively, meaning it counts and displays all the files and subdirectories in the main directory.
After running the tree command, you will see a tree-like structure and the total number of files at the end of the output.
By default, the tree command doesn’t include the hidden files. If you want to display and count hidden files also, you can run the tree command with the -a flag:
tree -a .
Now, the output will display all files along with hidden ones.
You can explore more options with the Linux tree command for file-counting purposes.
Argument | Description | Example |
-a | Show everything, including hidden files. | tree -a /home/user |
-d | List only directories, no files. | tree -d /home/user |
-f | Print the complete path in front of each file. | tree -f /home/user |
-i | Don’t use spaces to show levels in the directory structure. | tree -i /home/user |
-l | The symbolic links are treated as regular directories when displaying a directory structure. | tree -l /home/user |
-r | Sort the output in the opposite order (Z to A). | tree -r /home/user |
-t | Sort the output by the file’s date when they were last changed. | tree -t /home/user |
-x | Only show contents within the current file system; avoid crossing over. | tree -x /home/user |
-P | Include only files that match a pattern (like *.txt for text files). | tree -P '*.txt' /home/user |
-I | Skip files that match a pattern (like *.txt for text files). | tree -I '*.txt' /home/user |
We have now covered the tree command, which visually represents all files. To get more control over the file count system, try the find command in Linux.
4. Count the Number of Files Using find Command
As we covered earlier, the wc command with ls can count files in Linux. The Linux find command can also be piped with wc to count files.
The Linux find command provides multiple options and flexibility to count files according to your custom instructions. Let’s check some of the main flags that can be used with the Linux find command.
4.1. Counting All Files (Including Hidden)
To count all files present inside the main directory and subdirectories, you can use the find command with -type f flag and pipe it with the wc command:
find . -type f | wc -l
This command also counts the hidden files by default.
The output displays a total of seven, which means we have three text files and one hidden file inside the main test directory and two text files and one hidden file inside the subdirectory named subtest.
4.2. Counting Only Directories (Including Hidden)
If you only want to count directories, you can run this command:
find . -type d | wc -l
One thing to note is that we have only one subdirectory inside our current working directory, but the total count is two. This is because the find command also includes the main directory in its count and increases the total count by one.
4.3. Counting All Files (Excluding Hidden)
By default, the find command without specific flags includes hidden and regular files. To exclude the hidden files in the total count, you can run this command:
find . -type f ! -name ".*" | wc -l
We added the -name “*.*” flag to exclude hidden files starting with a dot (.). As mentioned earlier, the find command also counts the current directory as a separate entity, increasing the final count by one.
4.4. Counting All Directories (Excluding Hidden)
Similarly, if you need to count the directories only, you can run this:
find -type d -not -path '*/\.*' | wc -l
However, we don’t have any hidden directories here, so the total count will still be two.
4.5. Limit Your Files and Directories Count
By default, the find command recursively counts files inside all the directories and their subdirectories. But if you want to limit the search result to only the leading directory or one subdirectory, you have to add the maxdepth option with the find command. By specifying a number with the –maxdepth option, you can control how many levels of directories the find command should traverse.
For example, to count the number of files in the current directory (without checking subdirectories), run:
find -maxdepth 1 -type f | wc -l
You will get the total count of regular and hidden files inside the current directory.
It doesn’t count the files present inside the subdirectory, which, in our case, is the subtest directory.
To exclude the hidden files, run this:
find . -maxdepth 1 -type f ! -name ".*" | wc -l
Like maxdepth, the find command has the mindepth flag. This option specifies the minimum depth (number of levels) at which the search should apply tests or actions.
For example, this command will start the search at a minimum depth of two.
find -mindepth 2 -type f | wc -l
This means the search will exclude items present inside the current directory, immediately start from the subdirectories, and give the total file count.
4.6. Counting Specific File Types Using find Command
To count files with specific types, use the -name option with file extension to count files with particular types.
For example, if you’re interested in counting only .txt files, you can run the following command:
find . -type f -name "*.txt" | wc -l
The output of this command displays the overall count of .txt files in both the current directory and its subdirectories.
5. Counting Files in a Linux Directory Using Bash Script
Bash scripts in Linux are used for task automation. Why not use them to count files in the Linux directory with just one executable command? This allows us to create custom bash scripts to count files with specific requirements, just like a regular coding program.
First, create a new test script:
nano test.sh
Paste the following content inside the bash script file:
#!/bin/bash
dir="test"
count=$(find "$dir" | wc -l)
echo "Total files in the $dir directory are $count."
Give the necessary permissions to it:
chmod +x test.sh
Finally run, the script to count the number of files in the test directory:
./test.sh
This script counts the number of files and directories in the test directory and prints a message indicating how many files were found. It uses the find command piped with the wc command for file count. You can modify this line inside the scripts by following the above-mentioned cases. This will help you change the script to fit your needs.
Note: The output count is also increased by one because of the find command’s default behavior.
6. Counting Files Using GUI Method
The final method on the list is using the GUI file manager. This is the simplest way of counting files in Linux. It allows you to navigate and count files easily.
Simply open the directory where you want to count files.
Right-click the folder in which you need to count files and directories.
A new window will display the summary of files within that directory.
Remember, this total count is not the individual count of any file, folder, directory, or text file. However, it is the overall count of all the content inside the directory, including images, videos, and documents.
Point to Ponder
Does the GUI method also count the hidden files?
Yes, the GUI method includes the hidden files inside the total count.
Do the hidden files are still counted, even if they are hidden?
Yes, the GUI method counts the hidden files by default, regardless of whether they are shown or displayed.
7. File Counting Method Picker
Below, I have designed a custom method picker table based on my knowledge and experience to understand better which file counting method you used. It will help you evaluate all these above methods easily.
Case | ls | find | tree | Bash Script | GUI |
Quick file count in the current directory | Yes | Yes | |||
Counting files with specific criteria (extension, date, etc.) | Yes | Yes | |||
Counting files in subdirectories | Yes | Yes | Yes | ||
Large directory with complex filtering | Yes | Yes | |||
Visual overview of a directory structure with file counts | Yes | Yes | |||
User-friendly, point-and-click approach | Yes | Yes | |||
Automating regular file-counting tasks | Yes | ||||
Integrating file counting with other operations | Yes | ||||
Basic file counting for casual users | Yes | Yes |
We have covered all the main methods for file counting in Linux.
Conclusion
Counting files in Linux has multiple purposes, and it can help manage, analyze, and troubleshoot files and directories. Counting files helps you understand how much space files occupy on your storage devices. To count files in Linux, you can use the ls command piped with wc. If you want to count files and get an illustration of all files, you can try the tree command. Further, the find command provides more advanced features like extracting file count for a specific format. You can also create a bash script to count files in Linux.