Count total files or directories using bash script – Code Example

Total
0
Shares

In this article we will look at the bash script code which can count total files or directories inside any directory. This works by passing the output of the glob to the function and then counting the number of arguments.

Code Example –

1. Function –

#!/bin/bash

count_files_directories() {
    printf '%s\n' "$#"
}

2. Counting files –

count ~/Downloads/*

3. Counting directories –

count ~/Downloads/*/

4. Counting all png files –

count ~/Pictures/*.jpg

Inspired from Dylan Araps