Count Files in Linux

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Updated: July 11, 2024Published: July 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:

Bash
$ ls -1 <path> | wc -l

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:

Bash
$ ls -1 ~/server | wc -l
     5

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:

Bash
$ ls -1RA ~/server | wc -l
     265

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:

Bash
$ find <path> -type f | wc -l

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:

Bash
$ find ~/server -type f | wc -l
     59
$ find ~/server -type f -maxdepth 1 | wc -l
     5

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
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

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.

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.

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 Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

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.

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.