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

The Directory Size Problem

When managing an Ubuntu Linux server, you occasionally need to know exactly how many files are inside a specific directory. For example, if you run a website that generates a new temporary file for every user session, that folder might accumulate tens of thousands of files over a weekend. Opening a graphical file manager to count them will instantly crash the program, and simply typing ls in the terminal will generate a scrolling wall of text that takes minutes to finish. To get an exact numerical count instantly, you must combine two classic Unix commands using a “pipe.”

Using ls and wc Together

The standard way to count files is to list them and then pass that list directly into the “word count” (wc) utility.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. Navigate to the directory you want to count (e.g., cd /var/log/).
  3. Type the following command exactly as written:
  4. ls -1 | wc -l
  5. Press Enter.

Understanding the Syntax

  • ls -1 (That is the number one, not the letter L). This forces the list command to print exactly one file per line, rather than arranging them in wide columns.
  • | (The pipe character, usually found above the Enter key). This takes the output of the first command and feeds it secretly into the second command.
  • wc -l (Word Count with the “lines” flag). Instead of counting words, this specifically counts how many lines of text were handed to it.

The terminal will instantly return a single number (like 4521), representing the exact number of files and folders directly inside that directory.

Counting Subdirectories Recursively

If you want to count every file in a directory, plus every file buried deep inside all of its subfolders, the ls command isn’t powerful enough. You must use the find command instead:

  1. find . -type f | wc -l

This command searches the current directory (.) for files only (-type f) and counts them, giving you the absolute total file count for the entire directory tree.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.