Count Files in Linux
Razvan Ludosanu
Founder, learnbackend.dev
Updated: 7/11/2024
Published: 7/11/2024
The short answer
On Unix-like operating systems such as Linux and macOS, to count the number of files and folders contained in a specified directory, you can combine the ls and wc commands as follows:
$ ls -1 <path> | wc -l
Run in Warp
Where:
- The ls -1 command is used to output the files and directories in the form of a list.
- The wc -l command is used to count the number of lines outputted by the ls command.
- The path argument is the relative or absolute path to the desired directory.
For example:
$ ls -1 ~/server | wc -l
5
Run in Warp
Counting entries recursively
If you want to output the number of files and directories contained in the subfolders of the specified directory, including hidden files (i.e. dotfiles), you can use the -R (i.e. "recursive") and -A (i.e. "all") flags as follows:
$ ls -1RA ~/server | wc -l
265
Run in Warp
Counting regular files only
To output the number of regular files only contained in a specific directory, including its subdirectories, you can combined the find and wc commands as follows:
$ find <path> -type f | wc -l
Run in Warp
Where:
- The find -type f command is used to search for regular files only.
- The path argument is the relative or absolute path to the desired directory.
Note that you can limit the search depth of the find command using the -maxdepth flag.
For example:
$ find ~/server -type f | wc -l
59
$ find ~/server -type f -maxdepth 1 | wc -l
5
Run in Warp
Easily retrieve these commands using Warp's AI Command Suggestions feature
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering count regular files in folder in the AI Command Suggestions will prompt a find command that can then quickly be inserted into your shell by doing CMD+ENTER.
Written by
Razvan Ludosanu
Founder, learnbackend.dev
Filed Under
Related Articles
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
Remover Users in Linux
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.
Create Files In Linux
Learn how to create regular files in Linux using command line interface commands like touch, echo, cat, printf, and in-terminal text editors like Vim.