When working in a graphical file manager on Windows or macOS, you can easily see the total number of files in a folder by glancing at the status bar. However, when you are managing a headless Ubuntu Linux server via the terminal, obtaining a simple file count requires combining a few standard command-line utilities. Whether you are verifying a massive backup archive, auditing log files, or checking image uploads in a web directory, knowing how to quickly count files is an essential Linux skill.
The Core Concept: Piping Commands
In Linux, there is no single command like count-files. Instead, the Unix philosophy relies on combining small, single-purpose tools using a “pipe” (|). We will use one command to list the files, and pipe that list into a second command that simply counts the lines.
Method 1: Counting Files in the Current Directory (No Subfolders)
If you only want to know how many files are sitting directly inside the folder you are currently looking at (ignoring any subdirectories inside it), you will combine the ls (list) and wc (word count) commands.
- Open your terminal and navigate to your target folder using
cd. - Type the following command:
ls -1 | wc -l
How it works:
ls -1(that is the number one, not an “L”) lists the contents of the directory, forcing it to print exactly one item per line.|(the pipe) takes that vertical list and hands it to the next program.wc -l(word count, lines flag) simply counts the number of lines it received and outputs the final number.
Note: This basic command counts both files and folders present in the directory.
Method 2: Counting Only Files (Including All Subdirectories)
If you want a highly accurate count of only files (ignoring directories) and you want to recursively search through every folder inside your current location, you must upgrade from ls to the powerful find command.
Run the following command in your terminal:
find . -type f | wc -l
How it works:
find .tells the system to search starting in the current directory (represented by the period.).-type frestricts the search so it only outputs actual files, completely ignoring directories, symlinks, and sockets.wc -lcounts the resulting list.
Because the find command searches recursively by default, this will give you the grand total of every single file hidden anywhere inside the current folder structure.