≡ Menu

Shell Script To Count Number Of Files In Each Subdirectories

# Write a script that will count the number of files in each of your subdirectories.
# -------------------------------------------------------------------------
# Copyright (c) 2001 nixCraft project 
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# change your directory to command line if passed
# otherwise use home directory
[ $# -eq 1 ] && START=$1 || :
if [ ! -d $START ]
	echo "$START not a directory!"
	exit 1
# use find command to get all subdirs name in DIRS variable
DIRS=$(find "$START" -type d)
# loop thought each dir to get the number of files in each of subdir
for d in $DIRS
   [ "$d" != "." -a "$d" != ".." ] &&  echo "$d dirctory has $(ls -l $d | wc -l) files" || :

Comments on this entry are closed.

  • ghostdog74

    GNU find/awk

    find /path -printf "%h:%f\n" | awk -F: '{d[$1]++}END{ for(o in d) print d[o],o }'

  • Gagan

    Even easier-

    find -type f -execdir pwd \; | sort | uniq -c

  • Ashish Barot

    Hi Gagan,

    can you pl. explain the string which you had paste here.
    what is use case of “f” in your string?

    Ashish Barot.

  • Hi there..

    Love the script, just what I was looking for… Must admit that what someone posted ‘ find -type f -execdir pwd \; | sort | uniq -c ‘ was easier but would not even have found that if it was not for your site.


  • Magnus

    Big thanks! I’ve really been looking for this, most helpful!

  • Arnoques

    I like Gagan’s version, but is slow because it executes pwd (an external program) for each file. A faster version:

    find . -type f -printf '%h\n' | sort | uniq -c

    and, to sort by the number of files and only get the first ten you can add

    find . -type f -printf '%h\n' | sort | uniq -c | sort -nr | head

  • Greate man…
    superb post.. find command is very versatile…

  • You don’t need to sort here. This will work fine too
    find -type f -execdir pwd \; | uniq -c

  • Vivek

    Here is the Full Script
    DIRLIST=$(find “$1” -type d)
    for d in $DIRLIST
    [ $d != “.” -a $d != “..” ] && count=$(find “$d” -maxdepth 1 -type f | wc -l)
    printf “Directory name :$d $count \n”

  • Stephan

    find . -name .\* | wc -l
    (counts directories too)

    maybe better:
    find . -name .\* | grep -v “\.\$” | wc -l

  • Anonymous

    hi ashish f is here specifying that it should count only files not a directory.

  • Hmmm… getting greater and greater – find the error :-)
    crzydg@aitne ~ $ find -type f -execdir pwd \; | uniq -c |wc -l
    crzydg@aitne ~ $ find . -type f -printf ‘%h\n’ | sort | uniq -c |wc -l
    crzydg@aitne ~ $ find . -type f | uniq -c |wc -l
    crzydg@aitne ~ $ pwd

  • Dularl


    I learnt a lot from this, but i have a question. I would like to count the file types in each subfolder. For example Directory1 has 4 pdf 3 txt files Directory2 has 1 png 1jpg files.
    And i would like to do this without using awk.