This tutorial walks you through counting files in a directory in Linux using the
wc command with the ls and find commands, rsync command, tree command, and the GUI. It briefly discusses each command and its options and then shows you how to use it practically.
Let’s start by building a directory tree for practice.
Table of Contents
- Lab Setup to Practice Counting Files in Directory in Linux
Lab Setup to Practice Counting Files in Directory in Linux
Let’s build a file structure consisting of four directories and seven files. Head to your terminal and create the main directory with two subdirectories and three files.
mkdir main_directory cd main_directory mkdir first_subdirectory second_subdirectory touch file1.py file2.js file3.c
cd into the first subdirectory and create three text files.
cd first_subdirectory touch file4.txt file5.txt file6.txt
cd into the second subdirectory and create a sub directory and a text file.
cd .. cd second_subdirectory mkdir third_subdirectory touch file7.txt
Lastly, return to the main directory in readiness to start counting the files.
The ls counts files and directories, which you can then pipe into the
wc command to count the number of lines. Here are the typical ways to use the ls command with the
Count Files in the Current Directory
Count the files using the ls command, then pipe the output onto the
ls | wc -l
where -l tells the system to count the number of newline characters.
The system returns the sum of (2) subdirectories and (3) files at the first depth of the tree:
Count Files in the Specified File Path or Directory
Let’s specify the directory whose files we want to count after the ls command.
ls <directory name or full path> | wc -l
For example, we can count the files in the first subdirectory.
ls first_subdirectory | wc -l
The system returns the number of the (
3) text files in the first subdirectory: file4.txt, file5.txt, and file6.txt.
Include the Line for Total Disk Allocation in the Output
Assume we not only want to print the number of the current directory’s number of newlines but also include the current directory’s files’ total disk allocation line. We introduce the -l option after the ls command.
ls -l <directory name or full path> | wc -l
For example, let’s get the the first subdirectory’s files’ total disk allocation line plus its files’ number.
ls -l first_subdirectory | wc -l
Show Files Only
We can build a more complex command structure to count only files while ignoring directories. For example, let’s count the files in the main directory’s tree using the ls, grep, and the wc commands.
ls -1Up main_directory | grep -v / | wc -l
-1: returns one entry per line,
-U: returns an unsorted output,
-p: appends slash / indicator to directories, and
grep -v: excludes the directories.
The system returns the (3) files:
The main drawback of counting a directory’s files with the
wc commands is the search mainly ends at first tree depth. It would help if you learned to recursively count a directory’s files using the
find command lets you do a recursive search on all files, even in subdirectories. Besides, it offers you more control than the ls command. Let’s see the typical ways to use it when counting files.
Count All Files and Directories
Specify the directory name after the find command.
find <directory> | wc -l
find main_directory | wc -l
We get the total number of the seven files (
file7.txt), three subdirectories (
third_subdirectory) and the main directory.
Recursive File Count in the Specified Directory
Assume we want to recursively get the number of files in the main directory, excluding the directories. We can introduce the -type f option to list only files.
find main_directory -type f | wc -l
The system counts the files from the main directory and its subdirectories:
If you get a permission denied error message on running the find command a directory, redirect the output to
/dev/null before counting the returned lines.
find main_directory-type f 2> /dev/null | wc -l
Specify the viewable depth
Apart from recursively getting all files in the directory tree, you can control the depth. For example, we can limit the search to the main directory’s first level.
find main_directory -maxdepth 1 -type f | wc -l
-maxdepth 1 limits the search to the first level directory.
The file count ends on the first depth consisting of 3 files: file1.py, file2.js, and file3.c.
We can use the rsync backup tool to display the file counts and statistical data about the target directory.
rsync – stats – dry-run -a main_directory
--dry-run: prevents backing up the files,
--stats: shows the end statistics, and
-a: recursively counts the files.
Apart from listing down the files and directories, the system tells you their count.
The tree command provides a pretty view of the directory structure before returning the total file count.
The first step toward using the tool is to install it.
sudo apt install tree
sudo yum install tree
We can then use it to view unhidden and hidden files.
tree -a main_directory
main_directory ├── file1.py ├── file2.js ├── file3.c ├── first_subdirectory │ ├── file4.txt │ ├── file5.txt │ └── file6.txt └── second_subdirectory ├── file7.txt └── third_subdirectory 3 directories, 7 files
Apart from the above commands, you can count files using a desktop environment’s user interface. For example, let’s count the main directory’s files using Ubuntu’s GNOME editor.
Search files on the activities panel.
Locate the main_directory folder:
And right-click it to access the Properties.
Click on Properties to view the main directory’s details, like the number of files and directories.
You can count files in directory in Linux using native commands (
rsync), third-party tools (
tree), and the graphical user interface, as shown in this tutorial.