How to Count Files in a Directory in Linux

By
Sara Zivanov
Published:
June 30, 2025
Topics:

Counting files in a Linux directory is useful for monitoring system performance, estimating backup size, tracking data growth, and verifying automated processes. In large or complex directory structures, accurate file counts help identify anomalies or validate expected outcomes.

Linux offers several efficient methods for obtaining an accurate count, using both command-line tools and the Graphical User Interface (GUI).

This tutorial explains each command-line and GUI method through practical, hands-on examples.

How to Count Files in a Directory in Linux

How to Count Files in a Directory Using Command Line

Using the command line is often faster and more flexible than a graphical interface, especially when working with large directories, remote systems, or scripting tasks. It also supports advanced options that are unavailable in most graphical interfaces, such as:

  • Filtering by name or type.
  • Counting files recursively across subdirectories.
  • Excluding specific paths.

Before running the commands in this guide, create the following sample directory structure:

1. Use mkdir to create the main directory and two subdirectories:

mkdir -p testdir/dir1 testdir/dir2

This command creates a directory named testdir, which contains two subdirectories: dir1 and dir2.

2. Run touch to create four files in testdir:

touch testdir/file1.txt testdir/file2.log testdir/.hiddenfile testdir/script.sh

This creates four files in the top-level directory: file1.txt, file2.log, .hiddenfile, and script.sh

3. Use touch again to add files inside the subdirectories:

touch testdir/dir1/file3.txt testdir/dir1/file4.txt testdir/dir2/file5.md

This creates two files in dir1: file3.txt and file4.txt, and one file in dir2 named file5.md.

4. None of the commands above provides any output. To confirm the directory structure and files are created, use the tree command:

tree testdir
tree testdir terminal output

If tree is not available, use the ls command instead:

ls -R testdir
ls -R testdir terminal output

Note: The hidden file is not visible in the output.

This process results in a total of seven files:

  • Four files at the top level: file1.txt, file2.log, .hiddenfile, and script.sh.
  • Three files in subdirectories: file3.txt, file4.txt, and file5.md.

Linux classifies these files into different types:

  • Regular files. Standard data files such as text documents, scripts, or logs. All files in this structure, except the hidden one, fall into this category.
  • Hidden files. Files that start with a dot (.) and are excluded from most listings by default. Example: .hiddenfile.
  • Directories. Folders that contain files or other directories. In this example: dir1 and dir2.
  • Special entries. Each directory includes . (the current directory) and .. (the parent directory). Some commands count these unless explicitly filtered.

The following sections explain how to count files via the command line, depending on your use case.

How to Count All Files in a Directory 

Counting all files in a directory is useful for tasks like estimating storage usage, tracking changes, or validating file transfers. This count only includes regular files and skips files in subdirectories unless the recursive option is used.

There are several ways to complete this task via the command line:

Count All Files with wc and find Commands 

The find command searches for files matching specified criteria, while wc (word count) counts lines, words, or bytes in input. Combining find with wc -l counts the number of files.

The general syntax is:

find [directory-name] -type f | wc -l

For example, to count how many files are in testdir, run:

find testdir -type f | wc -l
find testdir -type f | wc -l terminal output

This command searches for all files (including hidden ones) in testdir and its subdirectories, and returns the total number of files without listing them.

Count All Files with wc and ls Commands 

The ls command lists directory contents, and when piped to wc -l, it counts the number of listed items. This method only works for files in the top-level directory and does not include subdirectories or hidden files by default. However, it does list subdirectories as it does not differentiate between files and directories.

The syntax is:

ls [directory-name] | wc -l

For example, to count how many files and directories are in testdir, run:

ls testdir | wc -l
ls testdir | wc -l terminal output

This command returns five, which includes file1.txt, file2.log, script.sh, and dir1, and dir2. The hidden file .hiddenfile is not included in the count.

Count All Visible Entries with Bash Globbing

Bash globbing, also known as filename expansion, wildcard matching, or pathname expansion, uses patterns to match visible files and directories directly within the shell. It's fast and efficient, but does not include hidden files or differentiate between file types.

The command syntax is:

files=([directory-name]/*); echo "${#files[@]}"

The command contains:

  • files=([directory-name]/*). Matches all visible files and directories inside [directory-name] and assigns them to an array called files.
  • "${#files[@]}". Returns the total number of matched entries in the array.
  • echo. Prints the output to the terminal.

The only part you need to change is the [directory-name].

Note: Bash array expansion stores multiple filenames matched by globbing into an array variable, which can then be counted using ${#files[@]}.

For example, to count files and directories in testdir, run:

files=(testdir/*); echo "${#files[@]}"
files=(testdir/*); echo "${#files[@]}" terminal output

This counts all visible files and directories inside testdir but excludes hidden files and contents of subdirectories. In this case, it counts five entries: file1.txt, file2.log, script.sh, dir1, and dir2.

How to Count Files Excluding Subdirectories 

In some cases, it is necessary to count only the files located directly within a directory, and skip files inside subdirectories. For instance, this helps when you want to measure or monitor the immediate contents of a directory without including nested files, which impact totals or misrepresent storage usage.

The find [directory] -type f | wc -l command counts all regular files, including those in subdiretories and hidden files, while ls [directory] | wc -l only counts visible entries in the top-level directory, including subdirectories but not their contents. These commands are covered in the Count All Files in a Directory section.

The following sections present several command-line methods for counting files while excluding the contents of subdirectories.

Count Files Excluding Subdirectories with find and -maxdepth Option

The find command searches for files matching specified criteria. Using the -maxdepth option limits the search to the specified directory level, excluding files in subdirectories. Combining this with wc -l counts the number of matching files.

The general syntax is:

find [directory-name] -maxdepth 1 -type f | wc -l

For example, to count how many files are directly in testdir (excluding files in its subdirectories), run:

find testdir -maxdepth 1 -type f | wc -l
find testdir -maxdepth 1 -type f | wc -l terminal output

This command returns the total number of regular files in testdir, excluding files within any subdirectories, which in this case is four (file1.txt, file2.log,.hiddenfile, and script.sh).

Count Files Excluding Subdirectories with ls and grep Commands

The ls command lists directory contents, and when combined with grep, it filters the output to count only files and exclude directories. Therefore, this method works well for quick checks but may produce different results depending on filename patterns.

The command syntax is:

ls -l [directory-name] | grep ^- | wc -l

For example, to count files directly in testdir, run:

ls -l testdir | grep ^- | wc -l
ls -l testdir | grep ^- | wc -l terminal output

This command filters lines starting with -, which represent regular files, and counts them. It excludes directories and hidden files because ls -l by default does not list files starting with a dot (.). In this case, the count is three (file1.txt, file2.log, and script.sh).

To include hidden files, use the -a option with ls (e.g., ls -la), as shown in later examples.

Count Files Excluding Subdirectories with fd Command

The fd command is a modern alternative to find, known for its speed and simpler syntax. Use it to count files in a directory while excluding subdirectory contents.

The fd command is not installed by default. To add it on Debian-based systems, run:

sudo apt install fd-find

Note: After installation, you need to run it as fdfind or create an alias: alias fd=fdfind.

The command syntax is:

fd -tf -d 1 . [directory-name] | wc -l

The command includes:

  • fd. Searches for files and directories with a simple syntax.
  • -tf. Limits the search to regular files only.
  • -d 1. Restricts search depth to one (top-level only).
  • .. Matches all filenames.
  • [directory-name]. The target directory.
  • wc -l. Counts the number of matched files.

To use this command, replace [directory-name] with the name of the target directory. For example, to count top-level files in testdir, run:

fd -tf -d 1 . testdir | wc -l
fd -tf -d 1 . testdir | wc -l terminal output

This command returns the number three, counting file1.txt, file2.log, and script.sh. It excludes hidden files, subdirectory contents, and directories themselves.

Count Files Excluding Subdirectories With Shell For Loop and Test Operators

A shell for loop is a control structure in Bash used to repeat commands for each item in a list. It's useful for scripting and automation tasks that require custom logic.

This method combines a for loop with a test operator to check if each item is a regular file. In this case, the **-f** operator tests whether a given item is a file (not a directory, symlink, or other type).

The method is more complex than previous examples but is useful when building scripts or when external tools like find or fd aren't available.

The command syntax is:

count=0; for file in [directory-name]/*; do if [ -f "$file" ]; then count=$((count+1)); fi; done; echo $count

The command contains:

  • count=0. Initializes a counter variable to zero.
  • for file in [directory-name]/*. Loops over each visible item in the directory.
  • if [ -f "$file" ]. Tests whether the item is a regular file.
  • count=$((count+1)). Increments the counter when the test passes.
  • echo $count. Prints the final count.

Note: The keywords do, then, fi, and done are part of the Bash loop and conditional syntax, delimiting blocks of commands. They are necessary for the structure but don't perform standalone actions.

The only part you need to change is the [directory-name]. For example, to count files in testdir, run:

count=0; for file in testdir/*; do if [ -f "$file" ]; then count=$((count+1)); fi; done; echo $count
count=0; for file in testdir/*; do if [ -f "$file" ]; then count=$((count+1)); fi; done; echo $count

This command counts three regular files in testdir: file1.txt, file2.log, and script.sh. However, it excludes hidden files and directories.

How to Count All Files Including Hidden Files 

Hidden files in Linux start with a dot (.). They often store configuration or system data and are excluded by default in most tools. Being able to count them ensures complete visibility, which is important for monitoring directory size, preventing missed backups, and auditing environments.

Therefore, the following sections present different methods for counting all files, including hidden ones.

Note: The find and wc method also includes hidden files in the count. However, since it was already covered in the Count All Files in a Directory section, it is not repeated here.

Count All Files Including Hidden Files with ls -a and wc

This method is a variation of the previously covered ls and wc approach. By adding the -a option to ls, the command includes hidden files. The result is a total count of all visible and hidden entries in the specified directory.

The command syntax is:

ls -a [directory-name] | wc -l

For example, to count all visible and hidden entries in testdir, run:

ls -a testdir | wc -l
ls -a testdir | wc -l terminal  output

This returns a count of eight, which includes regular files (file1.txt, file2.log, script.sh), a hidden file (.hiddenfile), two subdirectories (dir1 and dir2), and special entries ( ., which represents the current directory, and .., which represents the parent directory).

The command does not differentiate between files and directories; therefore, the total includes all top-level entries.

Count All Files Including Hidden Files with wc and grep on ls -a output

This method combines the ls command with the -a option to count all entries in a directory, including hidden files. However, it excludes directories, symbolic links, and special entries.

The command syntax is:

ls -la [directory-name] | grep ^- | wc -l

The command contains:

  • ls -la [directory-name]. Lists all files and directories, including hidden ones, with detailed information.
  • grep ^-. Filters the output to include only regular files (lines starting with -).
  • wc -l. Counts the number of filtered lines, representing the total number of regular files.

For example, to count all regular files (including hidden ones) in testdir, run:

ls -la testdir | grep ^- | wc -l
ls -la testdir | grep ^- | wc -l terminal output

This returns a count of four, which includes file1.txt, file2.log, script.sh, and .hiddenfile.

Count All Files Including Hidden Files with Bash Globbing

Bash globbing allows you to match filenames using simple patterns without relying on external commands. Therefore, it is efficient and works well in scripts or interactive shells.

A version of this method was previously used in the Count All Visible Entries with Bash Globbing section. However, with a small modification, it also includes hidden files in the count.

The command syntax is:

files=([directory-name]/.* [directory-name]/*); echo "${#files[@]}"

The command contains:

  • files=([directory-name]/.* [directory-name]/*). Matches all hidden files and all visible files and directories inside [directory-name], and stores them in the files array.
  • "${#files[@]}". Returns the count of all matched items in the array.
  • echo. Prints the output to the terminal.

Change the [directory-name] to run this command successfully. For example, to count all visible and hidden entries in testdir, run:

files=(testdir/.* testdir/*); echo "${#files[@]}"
files=(testdir/.* testdir/*); echo "${#files[@]}" terminal output

This returns a count of six, which includes regular files (file1.txt, file2.log, script.sh), the hidden file (.hiddenfile), and two subdirectories (dir1 and dir2). However, it excludes the special entries . and .. automatically.

Count All Files Including Hidden Files with find Command and -mindepth

The find command includes all matching files by default, which results in counting special entries, such as . and .. when searching hidden files. However, the -mindepth option prevents this by excluding matches at the top-level directory itself.

This method ensures an accurate count of all regular files, including hidden ones, excluding special entries.

The command syntax is:

find [directory-name] -mindepth 1 -type f | wc -l

The command contains:

  • find. Searches for items in the directory tree.
  • [directory-name]. The directory to search.
  • -mindepth 1. Excludes the top-level directory itself (and special entries . and ..).
  • -type f. Filters results to regular files only.
  • wc -l. Counts the number of matched files.

For example, to count all regular files, including hidden ones in testdir (excluding special entries), run:

find testdir -mindepth 1 -type f | wc -l
find testdir -mindepth 1 -type f | wc -l terminal output

This returns the total count of files in testdir and all its subdirectories, including hidden files, but excludes . and .. entries.

How to Count Specific Files 

Sometimes, you need to count only certain types of files, such as all .txt files, files matching a naming pattern, or files modified within a specific time range. In such cases, counting specific files is useful for scripting, validating automation, or analyzing content by file type or purpose.

To address these needs, all the following methods are variations of previously covered commands, adapted with specific filters such as filename patterns, extensions, or regular expressions to count only the desired files.

Note: While they sound similar, specific files are not the same as special entries like . (current directory) and .. (parent directory). This section focuses on filtering and counting regular files based on specific attributes, such as name patterns or file extensions.

Count Specific Files with find and Name Pattern

The find command allows precise file selection by filtering names with the -name option. This is especially useful to count files matching exact filename patterns or extensions across a directory and its subdirectories.

The command syntax is:

find [directory-name] -type f -name "pattern" | wc -l

The command consists of:

  • find. Searches the directory tree recursively.
  • [directory-name]. The target directory.
  • -type f. Limits results to regular files.
  • -name "pattern". Matches files by name or wildcard pattern.
  • wc -l. Counts the number of matching files.

For example, to count all text files in testdir and its subdirectories, run:

find testdir -type f -name "*.txt" | wc -l
find testdir -type f -name "*.txt" | wc -l

This command returns the total number of files with the .txt extension, including those in hidden directories or with hidden names. In this case, those are file1.txt, file3.txt, and file4.txt.

This method is case-sensitive. To perform a case-insensitive search, replace -name with -iname.

Count Specific Files with find and Regex Pattern

The find command also supports regular expressions (regex) for advanced filename matching. A regex is a search pattern used to match complex text strings. In this case, it lets you filter files based on more flexible naming rules than -name, such as partial matches or multiple extensions.

The command syntax is:

find [directory-name] -type f -regex '.*\.log' | wc -l

The command contains:

  • find. Searches for files recursively in the given directory.
  • [directory-name]. The target directory to search.
  • -type f. Limits results to regular files.
  • -regex '.*\.log'. Filters files that end in .log. The .* matches any path or filename, and \.log targets the extension.
  • wc -l. Counts the number of matching files.

For example, to count all .log files in testdir using a regex pattern, run:

find testdir -type f -regex '.*\.log' | wc -l
find testdir -type f -regex '.*\.log' | wc -l terminal output

This command returns the total number of files ending in .log, including those inside subdirectories or hidden folders. In this case, the output is one, counting file2.log.

Count Specific Files with ls and grep

This method uses the ls command to list directory contents and grep to filter the output by a specific pattern, such as a file extension. Therefore, it is a quick solution for counting specific files at the top level of a directory. However, it does not include files in subdirectories or hidden files.

The command syntax is:

ls [directory-name] | grep '\.sh$' | wc -l

The command includes:

  • ls [directory-name]. Lists visible entries in the specified directory.
  • grep '\.sh$'. Filters lines ending in .sh.
  • wc -l. Counts the number of matching lines.

For example, to count all .sh files in testdir, run:

ls testdir | grep '\.sh$' | wc -l
ls testdir | grep '\.sh$' | wc -l terminal output

This command returns one, since script.sh is the only visible file in testdir with the .sh extension. Hidden files and files in subdirectories are excluded.

Count Specific Files with Bash Globbing and wc

This method uses Bash globbing (filename expansion) to match files based on a pattern, such as extensions, prefixes, or substrings, and then counts them using wc -w. However, it only matches visible files at the top level of a directory. Therefore, hidden files and subdirectory contents are not included.

It works well for quick counts at the top level of a directory and does not require external tools.

The command syntax is:

echo [directory-name]/*pattern* | wc -w

The command consists of:

  • echo [directory-name]/<em>pattern</em>*. Expands to all matching files based on the pattern.
  • wc -w. Counts how many matches were returned by echo.

The pattern can be anything supported by Bash wildcards, such as:

  • *.sh. For all shell scripts.
  • file*. For all files starting with file.
  • *log*. For all files with log in the name.

For example, to count all .log files in testdir, run:

echo testdir/*.log | wc -w
echo testdir/*.log | wc -w terminal output

This command returns the number of .log files in testdir. It only counts visible files at the top level and does not include subdirectory contents or hidden files.

Count Specific Files with fd Command

The fd tool is a fast, user-friendly file search utility that simplifies recursive searches and supports advanced filters. Moreover, it offers built-in support for matching specific file extensions, making it ideal for counting targeted file types.

The command syntax is:

fd -e [extension] . [directory-name] | wc -l

The command includes:

  • fd. Searches for files using a simplified syntax.
  • -e [extension]. Filters results to files with the specified extension (e.g., -e txt for .txt files).
  • .. Placeholder for the search pattern (dot matches all files).
  • [directory-name]. The directory to search recursively.
  • wc -l. Counts the number of matching files.

For example, to count all .md files in testdir and its subdirectories, run:

fd -e md . testdir | wc -l
fd -e md . testdir | wc -l terminal output

This returns a count of one, matching the file5.md file located inside testdir/dir2.

By default, the command searches all levels of the directory structure. However, it excludes hidden files and directories. Use the --hidden option to include them and --exclude to skip specific files or directories.

For example, to count all .txt files in testdir, including hidden ones, run:

fd --hidden -e txt . testdir | wc -l
fd --hidden -e txt . testdir | wc -l   terminal output

The command returns a count of three, which includes file1.txt, file3.txt, and file4.txt located in testdir and its subdirectory dir1.

Count Files Matching Pattern with Shell For Loop

This method uses a for loop with pattern matching to count files that match a specific naming pattern or extension. It is especially useful in scripts or when additional logic is needed beyond what tools like find or fd offer.

The command syntax is:

count=0; for file in [directory-name]/*.ext; do [ -f "$file" ] && ((count++)); done; echo $count

The command contains:

  • count=0. Initializes the counter.
  • for file in [directory-name]/*.ext. Loops over all files in the specified directory that match the pattern (e.g., .log, .sh, data_*).
  • [ -f "$file" ]. Checks if the matched item is a regular file.
  • ((count++)). Increments the count for each match.
  • echo $count. Prints the final count.

Replace [directory-name] with your directory and .ext with the desired pattern.

For example, to count all .log files in testdir, run:

count=0; for file in testdir/*.log; do [ -f "$file" ] && ((count++)); done; echo $count
count=0; for file in testdir/*.log; do [ -f "$file" ] && ((count++)); done; echo $count terminal output

The command returns a count of one, which includes file2.log located at the top level of testdir. This method does not include hidden files or files inside subdirectories unless the pattern is adjusted accordingly.

How to Count All Files in a Directory Using a GUI

Using a graphical user interface (GUI) to count files in a directory provides an accessible and intuitive alternative to command-line methods. In particular, it is useful for users who prefer visual navigation, need a quick overview without typing commands, or are managing files in unfamiliar environments.

Additionally, GUIs often display additional context, such as file types, sizes, and previews, which aids in file management beyond just counting.

The following steps demonstrate how to count files in a directory using GNOME Files, the default file manager in many Linux distributions:

1. Launch the file manager from your applications menu.

2. Browse to the directory whose files you want to count.

testdir in GUI

3. Press Ctrl+A to select all visible files and directories in the directory.

4. Look at the bottom bar or the properties pane. GNOME Files displays the number of selected items, which corresponds to the total number of files and directories currently visible.

testdir in GUI count

To include hidden files, press Ctrl+H to toggle their visibility. Next, repeat steps three and four to do the count accordingly.

testdir in GUI count, hidden file included

GNOME Files does not differentiate between files and directories in the item count, which means there is no way to count files only using this GUI. Some advanced file managers may offer this feature.

Conclusion

This tutorial explained how to count files in Linux directories using various methods. It covered how to count all files, including or excluding hidden files, applying filters for specific file types, and excluding subdirectory contents when needed.

Next, learn how to find a specific file and copy both files and directories in Linux. 

Was this article helpful?
YesNo