• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Source Digit

Source Digit

Linux, Ubuntu Tutorials & News, Technology, Gadgets & Gizmos

  • Home
  • How To
  • Ubuntu
  • Instagram
  • Blogging
  • Android
  • WordPress
  • +More
    • Cloud Computing
    • Gadgets
    • Social Media
    • Technology
  • Write for Us
Home / How To / Count Number Of Files In Directory And Subdirectory In Linux Ubuntu

Count Number Of Files In Directory And Subdirectory In Linux Ubuntu

By Sourabh / October 6, 2023 Category: How To, Ubuntu

How to count number of files in directory and subdirectory in Linux Ubuntu. Here is the command to find number of files in a directory in Linux or count number of files in directory recursively.

Using ls and wc Commnad

The simplest command to count number of files in a directory and subdirectories is ls and wc command. To use it, simply run:

ls | wc -l

The wc counts the number of bytes, characters, whitespace-separated words, and newlines in each given FILE, or standard input if none are given or
for a FILE. ‘wc’ prints one line of counts for each file, and if the file was given as an argument, it prints the file name following the counts. If more than one FILE is given, ‘wc’ prints a final line containing the cumulative counts, with the file name ‘total’. The counts are printed in this order: newlines, words, characters, bytes, maximum line length.

Whereas the ‘ls’ lists information about files (of any type, including directories).

Note that when we run the command ls and wc with -l options, the command will count all the files and directories but not the hidden ones. To list the hidden files, use -A option with the ls command:

ls -A | wc -l

If you want to count the number of files, including hidden files, in the current directory, run the following set of command:

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

In the above command, -p with ls adds / at the end of the directory names. The -A with ls option will list all the files and directories, including hidden files but excluding . and .. directories. Whereas wc -l counts the number of lines.

Recursively Count Files in Directory

Ubuntu Linux users can use find command to recursively count files in directory:

find DIR_NAME -type f | wc -l

Linux users can also use tree command for displaying the number of files in the present directory and subdirectories:

tree -a

If you want to get the number of files in the current directory only but not the subdirectories, set the level to 1:

tree -a -L 1

Linux Ubuntu users can execute find command to count the number of files in a directory: The find command will get all the files first and then count them using the wc command. Execute the following command:

find directory_path -type f | wc -l

If you don’t want to count the number of files from the subdirectories, limit find command at level 1. Note that level 1 is used for the current directory.

You can execute the following command:

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

For those who are not aware, the ‘find’ command searches the directory tree rooted at each file name. This list of files to search is followed by a list of expressions describing the files we wish to search for.

Primary Sidebar

Latest Posts

Free AI Courses With Certificate For Beginners In India

Best AI Plugins For WordPress To Use In 2026

3 Commands To Check USB Connection In Linux Terminal

Linux Terminal Commands for Beginners With Examples

How To Password Protect A Folder In Linux Command Line

Best Download Managers for Ubuntu Linux (2026 Edition)

5 Best Free Keyword Research Tools for Bloggers in 2026

How To Check Linux 32 or 64-Bit Command Line

How To Delete And Remove Repository In Ubuntu Terminal

Fix Unexpected Inconsistency By Running Fsck Manually From Initramfs

© 2026 Source Digit • Linux, Ubuntu Tutorials & News, Technology, Gadgets & Gizmos

  • Home
  • About
  • Privacy Policy
  • Terms & Conditions
  • Contact Us
  • Sitemap