How to Count Number of Files in Linux


Introduction

Linux is a popular open-source operating system used by millions of users worldwide. One of most common tasks in Linux is to count number of files in a directory. Counting number of files is an essential operation when dealing with large data sets and is also useful when cleaning up system or performing other maintenance tasks. In this article, we will discuss various methods to count number of files in Linux, along with examples.

Method 1: Using ls command with option -l

The 'ls' command is one of most commonly used commands in Linux to list files in a directory. We can use '-l' option with 'ls' command to display detailed information about files in a directory, including number of files. number of files in a directory is displayed in first column of output.

For example, to count number of files in current directory, we can use following command −

ls -l | grep "^-" | wc -l

The above command lists all files in current directory and then uses 'grep' command to filter out only regular files (not directories or other types of files). Finally, 'wc' command is used to count number of lines in output, which gives us number of regular files in directory.

Method 2: Using find command

The 'find' command is a powerful command-line tool used to search for files and directories in a directory hierarchy. We can use 'find' command with '-type f' option to find only regular files in a directory and then use 'wc' command to count number of files.

For example, to count number of files in current directory, we can use following command −

find . -maxdepth 1 -type f | wc -l

The above command searches for all regular files (not directories or other types of files) in current directory (specified by '.'), and '-maxdepth 1' option tells 'find' command to search only current directory (not its subdirectories). Finally, 'wc' command is used to count number of lines in output, which gives us number of regular files in directory.

Method 3: Using tree command

The 'tree' command is a useful tool for displaying contents of a directory in a tree-like format. We can use '-f' option with 'tree' command to display only files in a directory and then use 'wc' command to count number of files.

For example, to count number of files in current directory, we can use following command −

tree -f . | grep -v "/$" | wc -l

The above command displays all files in current directory and its subdirectories in a tree-like format and then uses 'grep' command to filter out only files (not directories). Finally, 'wc' command is used to count number of lines in output, which gives us number of files in directory.

Method 4: Using ls command with option -1

The 'ls' command can also be used with '-1' option to display only filenames in a directory, one per line. We can then use 'wc' command to count number of files.

For example, to count number of files in current directory, we can use following command −

ls -1 | wc -l

The above command lists all files in current directory, one per line, and then uses 'wc' command to count number of lines in output, which gives us number of files in directory.

Method 5: Using shell expansion

Shell expansion is a feature of Linux shell that allows us to use wildcard characters to match filenames. We can use shell expansion with 'echo' command to count number of files in a directory.

For example, to count number of files in current directory, we can use following command −

echo *

The above command expands '*' wildcard to match all files in current directory and displays their names on screen. number of files is equal to number of names displayed.

Bonus Tip: Counting Files Recursively

Sometimes, we may want to count number of files in a directory and all its subdirectories. In such cases, we can use '-R' option with 'ls' or 'find' command to list files recursively and then use 'wc' command to count number of files.

For example, to count number of files in current directory and all its subdirectories using 'ls' command, we can use following command −

ls -Rl | grep "^-" | wc -l

The above command lists all files in current directory and its subdirectories and then uses 'grep' command to filter out only regular files. Finally, 'wc' command is used to count number of lines in output, which gives us number of regular files in directory and its subdirectories.

Similarly, to count number of files in current directory and all its subdirectories using 'find' command, we can use following command −

find . -type f | wc -l

The above command searches for all regular files in current directory and its subdirectories and then uses 'wc' command to count number of files.

Note − '-R' option can be resource-intensive and time-consuming, especially for large directory trees. Use it with caution.

Conclusion

Counting number of files in a directory is a common task in Linux, and there are several ways to do it. We have discussed five methods to count number of files in Linux, including using 'ls' command with option '-l' or '-1', 'find' command, 'tree' command, and shell expansion. Each method has its advantages and disadvantages, and choice of method depends on specific use case. With these methods, you can easily count number of files in any directory in Linux.

Updated on: 24-Mar-2023

22K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements