How to Count Files in a Directory in Linux

ls | wc -l

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.

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.

cd ..

Notes/Articles/How to Count Files in Directory in Linux/lab setup.png

ls

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 wc command.

Count Files in the Current Directory

Count the files using the ls command, then pipe the output onto the wc command.

ls | wc -l

where -l tells the system to count the number of newline characters.

Output
5

The system returns the sum of (2) subdirectories and (3) files at the first depth of the tree: first_subdirectory, second_subdirectory, file1.py, file2.js, and file3.c.

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
Output
3

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
Output
4

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.

return to the home directory
cd ..
count only files in main_directory
ls -1Up main_directory | grep -v / | wc -l

Where:

  • -1: returns one entry per line,
  • -U: returns an unsorted output,
  • -p: appends slash / indicator to directories, and
  • grep -v: excludes the directories.
Output
3

The system returns the (3) files: file1.py, file2.js, file3.c.

The main drawback of counting a directory’s files with the ls and 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.

find

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
Input
find main_directory | wc -l
Output
11

We get the total number of the seven files (file1.py, file2.js, file3.c, file4.txt, file5.txt, file6.txt, and file7.txt), three subdirectories (first_subdirectory, second_subdirectory, and 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

Output
7

The system counts the files from the main directory and its subdirectories: file1.py, file2.js, file3.c, file4.txt, file5.txt, file6.txt, and file7.txt.

Bonus trick

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

Notes/Articles/How to Count Files in Directory in Linux/redirect the output.png

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

Where -maxdepth 1 limits the search to the first level directory.

Output
3

The file count ends on the first depth consisting of 3 files: file1.py, file2.js, and file3.c.

rsync

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

where;

  • --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.

Notes/Articles/How to Count Files in Directory in Linux/file and directory counts.png

tree

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.

Ubuntu/Debian

sudo apt install tree

CentOS/RHEL

sudo yum install tree

We can then use it to view unhidden and hidden files.

unhidden
tree main_directory
hidden
tree -a main_directory
Output
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

Notes/Articles/How to Count Files in Directory in Linux/use tree.png

GUI

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.

Notes/Articles/How to Count Files in Directory in Linux/search files.png

Locate the main_directory folder:

Notes/Articles/How to Count Files in Directory in Linux/main directory.png

And right-click it to access the Properties.

Notes/Articles/How to Count Files in Directory in Linux/properties.png

Click on Properties to view the main directory’s details, like the number of files and directories.

Notes/Articles/How to Count Files in Directory in Linux/main directory contents.png

Conclusion

You can count files in directory in Linux using native commands (wc, ls, find, rsync), third-party tools (tree), and the graphical user interface, as shown in this tutorial.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash While Loop
Read More

Bash While Loop

In almost all programming languages, a loop is an easy and basic method used to repeat single or…