Shell Script To Count Number Of Files In Each Subdirectories

#!/bin/bash
# Write a script that will count the number of files in each of your subdirectories.
# -------------------------------------------------------------------------
# Copyright (c) 2001 nixCraft project <http://cyberciti.biz/fb/>
# 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.
# -------------------------------------------------------------------------
 
START=$HOME
 
# change your directory to command line if passed
# otherwise use home directory
[ $# -eq 1 ] && START=$1 || :
 
if [ ! -d $START ] 
then
	echo "$START not a directory!"
	exit 1
fi
 
# 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
do
   [ "$d" != "." -a "$d" != ".." ] &&  echo "$d dirctory has $(ls -l $d | wc -l) files" || :
done
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • Dularl Mar 9, 2012 @ 11:53

    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.

  • crzydg Jan 4, 2012 @ 14:57

    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

  • Stephan Dec 1, 2011 @ 14:29

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

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

  • Vivek Nov 2, 2011 @ 15:15

    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

  • suresh Aug 19, 2010 @ 12:25

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

  • Arnoques Jul 3, 2010 @ 18:35

    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

  • Magnus Feb 12, 2010 @ 23:33

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

  • Daniel Iversen Jul 9, 2009 @ 11:09

    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

  • Ashish Barot Mar 5, 2009 @ 18:04

    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.

    • Anonymous Dec 8, 2011 @ 16:06

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

  • Gagan Oct 16, 2008 @ 0:32

    Even easier-

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

    • Alex Sep 16, 2010 @ 14:44

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

  • ghostdog74 Sep 20, 2008 @ 13:08

    GNU find/awk

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.