Shell Script To Count Number Of Files In Each Subdirectories

by on September 19, 2008 · 13 comments

  1. #!/bin/bash
  2. # Write a script that will count the number of files in each of your subdirectories.
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2001 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # -------------------------------------------------------------------------
  10.  
  11. START=$HOME
  12.  
  13. # change your directory to command line if passed
  14. # otherwise use home directory
  15. [ $# -eq 1 ] && START=$1 || :
  16.  
  17. if [ ! -d $START ]
  18. then
  19. echo "$START not a directory!"
  20. exit 1
  21. fi
  22.  
  23. # use find command to get all subdirs name in DIRS variable
  24. DIRS=$(find "$START" -type d)
  25.  
  26. # loop thought each dir to get the number of files in each of subdir
  27. for d in $DIRS
  28. do
  29. [ "$d" != "." -a "$d" != ".." ] && echo "$d dirctory has $(ls -l $d | wc -l) files" || :
  30. done


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • 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?

    Regards,
    Ashish Barot.

  • Daniel Iversen

    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.

    Thanks,
    Daniel

  • 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

  • suresh

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

  • Alex

    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
    #!/bin/bash
    DIRLIST=$(find “$1″ -type d)
    for d in $DIRLIST
    do
    [ $d != “.” -a $d != “..” ] && count=$(find “$d” -maxdepth 1 -type f | wc -l)
    printf “Directory name :$d $count \n”
    done

  • Stephan

    easier:
    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.

  • crzydg

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

  • Dularl

    Hi!

    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.

Previous Script:

Next Script: