Count the Number of Files in a Directory in Linux

How to Count the Number of Files in a Directory in Linux

Counting files in a directory is a common administrative task in Linux. Whether you need to verify the number of files created by a program, monitor storage usage, or troubleshoot directory-related issues, Linux provides several commands to help. This article explains the steps to count files both directly inside a directory and recursively for all files within subdirectories.



TL;DR

1. Count Files in the Current Directory: Use ls with the wc command:

2. Count Files Recursively: Use find:

find . -type f | wc -l

3. More commands:

TaskCommand
Count files in the current directoryls -1
Exclude directoriesls -p
Count all files recursivelyfind . -type f
Count specific file typesfind . -type f -name "*.txt"
Use tree for a summarytree -i


Counting Files in the Current Directory

To count files located immediately inside a directory (one level deep), the ls command combined with wc works effectively.

Command:

  • ls -1: Lists files and directories one per line.
  • wc -l: Counts the number of lines, which corresponds to the number of files and directories.

Example:

Output:

This indicates there are 12 files and directories in the current directory.

Exclude Directories from the Count

If you want to count only files (excluding directories), use:

  • ls -p: Appends a / to directory names.
  • grep -v /: Excludes lines ending with /.


Counting Files Recursively in a Directory

To include all files in subdirectories, the find command is the most efficient tool.

Command:

  • find .: Starts searching from the current directory (.).
  • -type f: Restricts the search to files only.
  • wc -l: Counts the total number of files.

Example:

Output:

This indicates there are 52 files in the directory and its subdirectories.



Count Files with a Specific Extension

To count only files with a specific extension (e.g., .txt), modify the find command:


Counting Files Using tree

The tree command provides a visual representation of a directory’s structure and counts files recursively.

Command:

-i: Disables indentation for a simpler view.

Example:

The last line of the tree output includes the total count of files and directories.



Summary of Commands

TaskCommand
Count files in the current directoryls -1
Exclude directoriesls -p
Count all files recursivelyfind . -type f
Count specific file typesfind . -type f -name "*.txt"
Use tree for a summarytree -i

Best Practices for Counting Files

  1. Exclude Hidden Files: By default, hidden files (starting with .) are excluded by ls. Use ls -1a to include them.
  2. Handle Large Directories: For directories with millions of files, find is more efficient than ls.
  3. Filter Results: Combine commands with grep or find filters to count files matching specific criteria.


  1. GNU Coreutils Documentation: ls
  2. Linux Man Pages: find
  3. Linux Man Pages: tree

Leave a Reply

Your email address will not be published. Required fields are marked *