l have a directory called /data/ which contains two sub-directories: /data/train/ and /data/test/. train and test each contain 101 sub-directories (101 directories in train and 101 in test).

These directories contain jpeg images. I want to loop through all the directories to:

  1. Count the total number of images
  2. Count the number of images contained in each directory

3 Answers 3


Read man find, and do something like

find /data/train /data/test -type f -print | wc -l

for dirname in $(find /data/train /data/test -type d -print) ; do
    /bin/echo -e -n "$dirname\t"
    find $dirname -type f -print | wc -l

Python has appropriate utilities for such job built in already. As one-liner this would be done as so:

$ python -u -c 'from os import walk,path; print "\n".join([str(len(f)) +" "+r for r,d,f in walk(".")])'

Or as script as so:

#!/usr/bin/env python
import os
import sys
for root,dirs,files in os.walk(sys.argv[1]):

Notice that in script version you're meant to call script with directory as positional parameter:

$ ./count_files.py my_dir/

In pure bash:

for dir in /data/*/; do
    files=( "$dir"/*/* )
    printf "%s\t%s\n" "$dir:" "${#files[@]}"

The output would be something like:

/data/test:     5432
/data/train:    1234

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.