dark mode light mode Search
Search

5 Easy Ways to Count Files in Linux Using CLI

TL; DR

To count files in Linux using the command line, you can try the following five ways:

  1. Basic Files Count: Counts the total number of files and directories in the current directory with the ls | wc -l command.
  2. Directory Depth Count: Counts the total number of files in the current directory only by using the find . -maxdepth 1 -type f | wc -l command.
  3. Files Count with Specific Criteria: Counts the total number of files with the specific criteria, such as text files, with the ls *.txt | wc -l command.
  4. Automate File Count Tasks: Create a shell script to automate the file counting process by using the cd /path/to/directory and ls | wc -l commands in a .sh file.
  5. View and Count Files in a Tree Format: Use the tree directory-path command to display the directory tree structure and count the files in the specified directory and its subdirectories in a tree-like format.

Counting files can help identify directories or files that are taking up too much space, locate missing or duplicate files, and manage files efficiently. It is a simple yet effective way to manage, analyze, and troubleshoot files and directories in Linux.

Read the article to know more about how to count files in Linux with step-by-step methods and effective file-counting tips.

Counting files can help identify directories or files that have too many files, locate missing or duplicate files, and manage files efficiently. It is a simple yet effective way to manage, analyze, and troubleshoot files and directories in Linux.

But if you don’t know how to count files in Linux, you can learn it through this article. I’ve covered five ways to count files in Linux using the command line. This includes from the basic to the advanced commands, along with the four quick tips to make the file counting process in Linux even more efficient.

How to Count Files in Linux [5 Simple Ways]

To count files in Linux using the command line, you can use the basic files count command, directory depth count command, files count with specific criteria, automated file count tasks, and view and count files in a tree-like format. Let’s learn each of these methods to count files in Linux here:

1. Basic Files Count Command

The most basic method for counting files in Linux is to use the ls command. This command lists the files in a directory and provides information on each file, including its size, permissions, and ownership. Here are detailed steps to use this command to count files in Linux using CLI:

  1. Navigate to the directory using the cd command.
navigate to the directory
  1. To count the number of files in a directory using ls, you can use the following command:
ls | wc -l
  1. The command ls | wc -l lists all the files and directories in the current directory and pipes the output to the wc -l command, which counts the number of lines in the output. This will give you the total number of files and directories in the current directory, including any subdirectories.
lists all the files in the current directory

2. Directory Depth Count Command

Another basic command to count files in Linux is the find command. This command searches for files in a directory hierarchy and can be used to count the number of files in a directory. Here’s how you can use implement this method:

  1. To count the number of files in a directory using find, you can use the following command:
find . -maxdepth 1 -type f | wc -l
  1. This command find . -maxdepth 1 -type f | wc -l uses the find command to search for all files in the current directory (specified by the “.“), but limits the search to a maximum depth of 1 (only files in the current directory, not in any subdirectories) and only consider files (not directories or other types of files). The output is then piped to the wc -l command to count the number of lines in the output and give you the total number of files in the current directory only.
command to search for all files

3. Files Count with Specific Criteria

If you need to count files in Linux with specific criteria, such as by file type or modification date, you can use the ls command line options with flags or options to specify criteria for file counting. To do so, follow these steps:

  1. To count only .txt files in a directory, you can use the following command:
ls *.txt | wc -l
  1. This command uses the wildcard character * to match all files with a .txt extension and wc to count the number of lines in the output.
command to count files in linux with specific criteria

4. Automate File Count Tasks

To streamline and automate counting files in Linux, create a shell script that executes the required commands and generates the desired output. Follow the step-by-step guide below to simplify the process:

  1. To count the number of files in the /home/user/Documents directory, create a script with the following code :
#!/bin/bash
cd /home/user/Documents
ls | wc -l
  1. To execute the script, you can save it to a file with a .sh extension, like count_files.sh. Then, make it executable using the chmod command. Your command should look like this:
chmod +x count_files.sh
  1. Then, run this file using the following command in the Terminal app.
./count_files.sh
  1. Once done, this script will count files and return the following output:
to execute the script

5. View and Count Files in a Hierarchical Format

The tree command is a recursive directory listing command that displays the contents of a directory and its subdirectories in a tree-like format. Here’s how you can use this command to count files in Linux:

  1. In the Linux command prompt, navigate to the directory using the cd command.
linux command to navigate to the directory
  1. Execute the tree command in the Terminal window, just as written below:
tree
  1. It will display the directory tree structure starting from the specified directory. As you can see, the output shows all the subdirectories and files within the specified directory and its subdirectories in a hierarchical tree-like format. Additionally, it’ll display the total files counted in the directory.
tree structure from the specified directory
  1. Alternatively, you can use the tree command just with the directory path for counting files in a directory.
tree directorypath
  1. You’ll notice it has the same output as the one executed in earlier steps.
tree command with the directory path

4 Quick Tips to Count Files in Linux

To count files in Linux more efficiently and optimize your overall file management workflow, try these four practical tips:

1. Add Wildcards to the ls Command

Wildcards feature in Linux, represented by an asterisk (*), serves as a powerful tool in file search and organization. It allows you to match multiple files based on your desired criteria. That is, you can use wildcards to find files in Linux that match specific patterns or even extensions. Here’s what your command should look like:

ls *.txt

By executing this command, you can locate and display all files that have a .txt extension in a directory. In this way, you can quickly and efficiently search and organize files on your Linux system.

match files with specific extensions

2. Run tee and ls Commands

The tee command is a useful utility that allows you to redirect the output of a command to multiple destinations simultaneously. It is particularly handy when you want to both view the output on your screen and store it in a file. This ensures that you have a permanent record of the directory’s contents, which can be useful for later analysis, reference, or sharing with others. To use this command, execute the following command line in the Linux command prompt:

ls | tee file_list.txt

When this command is executed, the output of the ls command is passed as input to tee. The tee command, in turn, creates two streams: one stream sends the output to the screen (standard output), and the other stream saves it to the specified file (file_list.txt) in the current directory. As a result, you will be able to observe the directory listing displayed on your screen and saved in the text file.

saves them to a file named file list txt

3. Use File Properties to Count Files

In Linux, you can use the graphical interface, which offers a more user-friendly approach to viewing the total number of files in a directory. In this way, it enables you to have easy access to file properties and provides a quick view of the directory’s contents. This method is ideal for users who prefer visual tools over command-line operations.

To count files using this method, simply right-click an empty space within the directory window, select Properties from the context menu, and locate the number of items displayed next to the Contents label. Now, you can easily view the total number of files along with the total size of the directory.

using a graphical interface to view files

4. Execute watch and ls Commands

The watch command in Linux is a valuable tool for monitoring file counts in real-time, as it helps to track changes or updates within a directory more efficiently. By constantly refreshing the output, it enables you to identify new files, deletions, or modifications as they happen. To utilize this command, execute the following in the Linux command prompt:

watch -n 1 ls -l

In the above command line, watch executes the ls -l command every 1 second and displays the output on the screen, allowing you to see any changes that occur in the directory as they happen.

to identify changes in a directory

To exit the watch interface in Linux, you can use the keyboard shortcut Ctrl + C. This will stop the watch command and return you to the command prompt.

To Sum Up


In conclusion, mastering the various methods to count files in Linux using the command line is a valuable skill for anyone working with files in the Linux environment. These techniques can help you manage files more effectively and troubleshoot any issues related to file and directory organization.

To further enhance your knowledge of Linux file management, consider exploring related articles on concatenating files, running binary files, and using chmod 755 to change file permissions for better security of your data. By doing so, you will gain a comprehensive understanding of Linux file management and work more efficiently within the Linux environment.

Frequently Asked Questions

What should I do if the file counting command in Linux returns an error?

If you encounter an error while using a file counting command, double-check the syntax and make sure you are using the correct command for your specific task. If you continue to encounter errors, check the command’s documentation or seek assistance from Linux forums or communities.

How can I count hidden files in Linux?

To count hidden files in Linux, you can use the command below:
find /path/to/directory -maxdepth 1 -type f -name ".*" | wc -l
This command searches for files in the specified directory (/path/to/directory) up to a maximum depth of 1 (only in the specified directory, not in any subdirectories), with a type of f (only files, not directories), and a name that starts with a dot (hidden files). The output is then piped to the wc -l command to count the number of lines in the output, giving you the total number of hidden files in the specified directory.

Can I count files across multiple directories at once?

Yes, you can count files in Linux across multiple directories at once using the find command. For example, to count all .txt files in the /home/user/Documents and /home/user/Pictures directories, you can use the following command:
find /home/user/Documents /home/user/Pictures -type f -name "*.txt" | wc -l

How do I count the number of lines in a file in Linux?

The wc (word count) command in Linux is used to count the number of lines, words, and characters in a file. To count only the number of lines, you can use the wc command with the -l flag, followed by the name of the file you want to count the lines in.
wc -l filename.txt

What is the difference between file count and disk usage?

In Linux, file count and disk usage are two separate concepts that are both important to understand when managing files. File count refers to the number of files in a particular directory, while disk usage refers to the amount of disk space those files are taking up. Although the two are related, they measure different aspects of file management. Knowing both the file count and disk usage can help users better understand the content of a directory, monitor disk space usage, and make informed decisions about how to manage files.

Total
0
Shares