• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Count Files in Linux

Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Updated: 7/11/2024

Published: 7/11/2024

About Terminus

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:

Thumbnail for Thumbnail for Thumbnail for Thumbnail for

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

Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

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.

UnixLinux
Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

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.

BashUnixLinux
Thumbnail for Neeran GulThumbnail for Neeran GulThumbnail for Neeran GulThumbnail for Neeran Gul
Neeran Gul

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.

Linux

Create Groups In Linux

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

Linux

Switch Users In Linux

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

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.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

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.

Linux

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.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for nullThumbnail for nullThumbnail for nullThumbnail for null