Count number of sub directories in directories

Posted on 2009-12-18
Last Modified: 2013-12-26
I am looking for an easy way to get a count of sub directories within an output of directories. Basically I am trying to get a count of users per customer. They each have a directory:


then under that they have users


I am trying to get an easy command that will output the customer name (/home/customer/) with the number of user directories under that.

I am just using "ls -l /home/*" which prints out the user directories which I then have to manually count. I am sure there is a much easier way.

Thanks in advance!
Question by:GCaron
    LVL 5

    Expert Comment


    find ./ -type d|wc

    where ./ is the subdirectory you want to count. The first or second number should be the folder count.

    Author Comment

    That looks to work if I'm in the customers directory but I have 150+ customers and don't want to have to go to each one and issue the command.

    I have


    I would like to issue one command to list out the number of users in each customer directory.
    LVL 5

    Expert Comment

    Oh, and it also counts the "hidden" folders and the . and .. things
    LVL 40

    Expert Comment


    cd /home
    for dir in *
       cd $dir
            echo "number of users under $dir is `ls | wc`"
       cd ..
    LVL 68

    Accepted Solution

    for dir in $(find /home -type d -maxdepth 1 -name "custom*") ; do echo $dir  "-->" $(ls $dir |wc -l); done

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Title # Comments Views Activity
    string initialization in java 11 94
    java ^ examples 8 48
    exchange 2007 3 44
    mapBully challenge 6 51
    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now