Count total files or directories using bash script – Code Example


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 –


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