Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

directory_name
# 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
add comment

2 Answers

up vote 2 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:

#!/usr/bin/ksh

# pass in the directory to search on the command line, use $PWD if not arg received
rdir=${1:-$(pwd)}

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

# 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"; 
    fi
done

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? –  user815423426 Oct 17 '11 at 22:21
1  
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. –  user815423426 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
add comment

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;
done
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
1  
@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
add comment

Your Answer

 
discard

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.