how to get number of files in a directory in linux code example

Example 1: bash command to find the number of files in a directory

ls -1q  | wc -l

Example 2: linux count files in subdirectories

find . -maxdepth 1 -type d | while read -r dir
do printf "%s:\t" "$dir"; find "$dir" -type f | wc -l; done

Example 3: list number of files in each folder linux

find . -type d -print0 | while read -d '' -r dir; do
    files=("$dir"/*)
    printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done

Example 4: find number of files in a directory linux

find -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" -type f | wc -l; done