From: rvaede on

I have a directory with a bunch of subdirectores.

how can count the number of files in the subdirectories using the wc -
l and provide me a listing like

directory1 12 (files)
dircetory2 15 (files)

etc..

regards
Roger
From: Tim Harig on
On 2010-06-10, rvaede <rvaedex23(a)gmail.com> wrote:
>
> I have a directory with a bunch of subdirectores.
>
> how can count the number of files in the subdirectories using the wc -
> l and provide me a listing like
>
> directory1 12 (files)
> dircetory2 15 (files)

You mean something like:

find ./* -type d -prune -print | while read x; do
printf "%s: " "`printf "$x" | tr -d '\n'`";
printf "%s" "`find "$x" -type f -print | wc -l`";
printf " (files)\n";
done
From: Chris F.A. Johnson on
On 2010-06-10, rvaede wrote:
>
> I have a directory with a bunch of subdirectores.
>
> how can count the number of files in the subdirectories using the wc -
> l and provide me a listing like
>
> directory1 12 (files)
> dircetory2 15 (files)

dir=/path/to/dir

for subdir in "$dir"/*/
do
set -- "$subdir"/*
printf "%s %d\n" "$subdir" "$#"
done

That ignores dot files, but adding them is easy.

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: Tim Harig on
On 2010-06-10, Tim Harig <usernet(a)ilthio.net> wrote:
> On 2010-06-10, rvaede <rvaedex23(a)gmail.com> wrote:
>>
>> I have a directory with a bunch of subdirectores.
>>
>> how can count the number of files in the subdirectories using the wc -
>> l and provide me a listing like
>>
>> directory1 12 (files)
>> dircetory2 15 (files)
>
> You mean something like:
>
> find ./* -type d -prune -print | while read x; do
> printf "%s: " "`printf "$x" | tr -d '\n'`";
> printf "%s" "`find "$x" -type f -print | wc -l`";
> printf " (files)\n";
> done

Note that this recursively prints all of the files (but not folders) under
each of the subdirectories. You can change that to get exactly what you
are looking for by changing the options to the second find call.

A (little) cleaner table format can be created like:

find ./* -type d -prune | while read x; do
printf "%s:" "`printf "$x" | tr -d '\n'`";
printf "\r\t\t %s" "`find "$x" -type f -print | wc -l`";
printf " (files)\n";
done

You can vary the indent based on the actual files to get it to work without
truncating long file names; but, that is beyond the scope of your question.