How do you print number of files for each folder in a directory [Linux]

I have been annoyed by the fact that I couldn’t easily print file count for all of the folders in certain directory.  Most of the time I just want to see what space each folder is using (du -hs *) but there are times when I need to know how many files are in each folder (checking cache folder, session folders etc).   So I whipped together a command line which does just that for me:

for i in `find -maxdepth 1 -type d`; do  echo -n $i " ";find $i|wc -l; done

I am sure there are many different ways to show file count for each folder in a directory and I am curious to see what people do so please do post comments with what you do.

Above command is pretty simple and can be expanded to do whatever you need.  For example, you can throw it into a bash script and be able to pass parameters.  For example:  count_files /home/  In this case your command line would look like:

for i in `find /home/ -maxdepth 1 -type d`; do  echo -n $i " ";find $i|wc -l; done

only difference would be that /home/ would be argument you passed and therefore will be $1.  Here is a sample script for above example:

#!/bin/bash
for i in `find $1 -maxdepth 1 -type d`; do
echo -n $i " ";
find $i|wc -l;
done

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

14 thoughts on “How do you print number of files for each folder in a directory [Linux]

  1. Sunny Walia Post author

    Thanks Scott but that gives disk usage for that particular folder which is not what I was looking. I was looking # of files within each folder under specific directory.

  2. bags on sale

    The cost of enrolled today is indeed high that nearly everyone has to
    adopt out student loans to drag it well bags on sale the proof is underneath the hood the internal components
    fit together well inside the truth and so are apple-labeled.

  3. te

    If some one wishes expert view about blogging and site-building then i suggest him/her to visit this
    blog, Keep up the pleasant job.

  4. AmeeSmall

    I have checked your website and i have found some duplicate content, that’s why
    you don’t rank high in google’s search results, but there is a tool that
    can help you to create 100% unique articles, search for; Boorfe’s
    tips unlimited content

  5. aktortogel

    I think everything posted made a bunch of sense.
    However, consider this, suppose you wrote a catchier post title?
    I mean, I don’t wish to tell you how to run your website,
    but suppose you added a headline that makes people want more?
    I mean How do you print number of files for each folder in a directory [Linux] |
    Technology: Learn and Share is a little plain. You could glance at
    Yahoo’s front page and see how they create article titles to get viewers interested.
    You might try adding a video or a pic or two to get readers excited about what you’ve got to say.
    In my opinion, it would bring your blog a little livelier.

  6. telek?z

    Great blog you have got here.. It’s difficult to find good quality writing like yours these days.
    I really appreciate individuals like you! Take care!!

Leave a Reply

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