Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I came up with the following snippet for counting files in each subdirectory:

for x (**/*(/)); do print $x; find $x -maxdepth 1 -type f | wc -l; done

The command outputs consecutive pairs (one below the other) as follows:

# of files

I would like to change the code above to:

  • Print each match on the same line (i.e. directory_name ':' # of files)
  • Only count files if the folders are leaves in the directory tree (i.e. they don't have any subfolders).

How can I do that?

share|improve this question
up vote 3 down vote accepted

try this:

for dir in $( gfind . -type d -print ); do files=$( find $dir -maxdepth 1 -type f | wc -l ); echo "$dir : $files"; done

or, in a script, where you can have a bit more flexibility:


# pass in the directory to search on the command line, use $PWD if not arg received

# if $rdir is a file, get it's directory
if [ -f $rdir ]; then
    rdir=$(dirname $rdir)

# first, find our tree of directories
for dir in $( gfind $rdir -type d -print ); do
    # get a count of directories within $dir.
    sdirs=$( find $dir -maxdepth 1 -type d | wc -l );
    # only proceed if sdirs is less than 2 ( 1 = self ).
    if (( $sdirs < 2 )); then 
        # get a count of all the files in $dir, but not in subdirs of $dir)
        files=$( find $dir -maxdepth 1 -type f | wc -l ); 
        echo "$dir : $files"; 

I use ksh for shell scripts, but it works just as well with #!/usr/bin/zsh, or /usr/bin/bash.

share|improve this answer
Thanks @Tim. What is gfind? – Amelio Vazquez-Reina Oct 17 '11 at 22:21
find. some systems (like Solaris) which use their own implementation of find, make the GNU tools available by prefixing them with a g. as in, gfind, gmake, gfind, etc. For use on Linux, or other which default to GNU versions of tools, simply change gfind to find. – Tim Kennedy Oct 18 '11 at 3:18
Thanks @Tim. Does the script above count files only on those folders that are leaves of the directory tree? (i.e. folders that don't have any subfolders)? It looks like it counts the files on every subfolder. – Amelio Vazquez-Reina Oct 18 '11 at 15:06
@intrpc, ah, sorry. i was printing totals for all files, by directory. I've edited it to only print the count of files only for directories that do not themselves include another directory. – Tim Kennedy Oct 18 '11 at 21:13

quick 'n' dirty

find . -type d | \
while IFS= read -r d; do
    f=$(ls -F "$d");
    echo "$f" | egrep -q "/$" || \
        echo $d : $(echo -n "$f"|wc -l) files;
share|improve this answer
very nice! way more elegant than mine, and gives exactly the same output ( if you just swap find . -type d with find ${1:-$(pwd)} -type d ). – Tim Kennedy Oct 19 '11 at 17:29
@TimKennedy, thanks. It's not a script though, just a one-liner, that's why $1 is not taken in to account. – forcefsck Oct 20 '11 at 8:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.