Ubuntu – how to get a list of folders and number files in each folder side by side, in the current directory

command linefiles

I am at the directory:
/home/john/my_test_files/
and under the directory there are 100 folders:

folder1
folder2
folder3
...
folder100

what command would I use to print the list of folders and the each one's file count side by side. Something like this:

folder1     25
folder2     78
folder3     34
...
folder100   88

where folder1 has 25 files in it, folder2 has 78 files in it, etc

I'm sure this is something basic but I couldn't find a good answer…

Best Answer

Here is one relatively slow solution, that provides a nice output :) Place the following function at the bottom of your ~/.bashrc file. Then open new terminal window or do source the run commands file: . ~/.bashrc.

count_in() {
    # set the current directory as default value
    local paths="$PWD"; echo
    # read the user's input as array when it is provided
    [[ ! -z ${@+x} ]] && local paths=( "$@" )

    # loop over the user's input
    for path in "${paths[@]}"
    do
        # test whether this is a directory
        [[ ! -d $path ]] && { echo -e "'$path' is not a directory.\n"; break; }

        # output color table header for each top level directory
        printf '\e[1;34m%-6s%-6s%-6s%s\e[m\n' "total" "files" "dirs" "analyzed directory"
        # analyse the top level data
        total=$(find "$path/" -mindepth 1 -maxdepth 1 | wc -l)
        files=$(find "$path/" -mindepth 1 -maxdepth 1 -type f | wc -l)
        dirs=$(find "$path/" -mindepth 1 -maxdepth 1 -type d | wc -l)
        # outpot the top level data
        printf '%-6d%-6d%-6d%s\n' "$total" "$files" "$dirs" "$path"

        # output color table separator for the inner directories
        printf '\e[1;96m%-6s%-6s%-6s%s\e[m\n' "total" "files" "dirs" "sub dir name"
        # for each inner directory
        while IFS='' read -r dir
        do
                # analyse a inner directory's data
                total=$(find "$dir" -mindepth 1 -maxdepth 1 | wc -l)
                files=$(find "$dir" -mindepth 1 -maxdepth 1 -type f | wc -l)
                dirs=$(find "$dir" -mindepth 1 -maxdepth 1 -type d | wc -l)
                # outpot the inner directory's data
                printf '%-6d%-6d%-6d%s\n' "$total" "$files" "$dirs" "$(basename "$dir")"
        done <<< $(find "$path/" -mindepth 1 -maxdepth 1 -type d -print)
        echo
    done
}

Then use the count_in function as shell command:

count_in  # will analyse the current directory
count_in  /path1 /path2

Sample output:

spas@Desktop:~$ count_in Pictures ~/Videos 'Something else'

total files dirs  analyzed directory
9     3     6     Pictures
total files dirs  sub dir name
19    19    0     Life Hacks
6     6     0     GIF
55    54    1     Wallpapers
20    18    2     Avatars
173   31    142   Photos
3     2     1     Icons

total files dirs  analyzed directory
6     0     6     /home/spas/Videos
total files dirs  sub dir name
66    0     66    Movies
11    0     11    Documentary.and.Conspiracy
7     7     0     .fun
16    2     14    Science.and.SciFi
2     2     0     Fun
2     1     1     Audio.Books

'Something else' is not a directory.

enter image description here