Jan 6, 2023 3 min read

How to Count Number of Files in a Directory in Linux

Here are several ways to count the number of files in a given directory in Linux.

Table of Contents

How do you know how many files are there in a directory?

In this quick tutorial, you'll learn various ways to count the number of files in a directory in Linux.

Method 1: Use ls and wc command for counting the number of lines in a directory

The simplest and the most obvious option is to use the wc command for counting number of files.

ls | wc -l

The above command will count all the files and directories but not the hidden ones. You can use -A option with the ls command to list hidden files but leaving out . and .. directories:

ls -A | wc -l

If you only want to count the number of files, including hidden files, in the current directory, you can combine a few commands like this:

ls -Ap | grep -v /$ | wc -l

Let me explain what it does:

  • -p with ls adds / at the end of the directory names.
  • -A with ls lists all the files and directories, including hidden files but excluding . and .. directories.
  • grep -v /$ only shows the lines that do NOT match ( -v option) lines that end with /.
  • wc -l counts the number of lines.
Count only number of files in a directory in Linux

Basically, you use ls to list display all the files and directories (with / added to directory names). You then use pipe redirection to parse this output to the grep command. The grep command only displays the lines that do not have / at the end. The wc command then counts all such lines.

List Only Directories in Linux With ls and Other Commands
Listing the contents of a directory is easy. But what if you want to list only the directories, not files and links?

Method 2: Use tree command for counting the number of files in a directory

You can use the tree command for displaying the number of files in the present directory and all of its subdirectories.

tree -a
Count number of lines in directory in Linux

As you can see, the last line of the output shows the number of directories and files, including the hidden ones thanks to the option -a.

If you want to get the number of files in the current directory only, exclude the subdirectories, you can set the level to 1 like this:

tree -a -L 1
Show number of files in current directory in Linux using the tree command

Method 3: Use find command to count the number of files in a directory

The evergreen find command is quite useful when it comes to dealing with files.

If you want to count the number of files in a directory, use the find command to get all the files first and then count them using the wc command.

find directory_path -type f | wc -l

With -type f you tell the find command to only look for files.

If you don't want the files from the subdirectories, limit the scope of the find command at level 1, i.e. current directory.

find . -maxdepth 1 -type f | wc -l
Counting the number of files in a directory with find command

There could be some other ways to count the number of lines in a directory in Linux. It's up to you how you want to go about it.

I hope you find this helpful. Feel free to leave a question or suggestion in the comment section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.