It is currently Tue, 19 Jun 2018 21:05:12 GMT

Author Message
 KSH for loop question

Can anyone tell how how to do the following in a ksh for loop or in
any other shell scripting form:

I have a list of directories (let's say dir1 thru dir10) with lots of
file in each one. These directories don't have subdirectories, only
files inside them. I want a script to be able to report back the
following statistics:
a) total number of directories
b) total number of files
c) a list of the directory names
d) total size in kbytes of directories and files
e) number of files in each of the directories

So far I have a script that can do a,b,c and d from above. Here's what
I have:
echo ""
echo "Number of Directories:`ls -Rl | grep "^d" | wc -l`"
echo "Number of Files:`ls -Rl | grep "^-" | wc -l`"
echo "Name of Directories: `ls -l | grep "^d" | awk '{print $9}'`"
echo "Total Size: `du -sk`"

How can I do e??
Basically, I want the output to look something like this:
dir1: 12 files
dir2: 100 files
dir10: 250 files

I think there might be a way to do this with a for loop with something
DIRLIST=`ls -l | awk '{print $9}'`
for i in $DIRLIST; do cd $i (and echo $i and output of "ls -l $i | wc
Something like that.

Any help is greatly apppreciated.

Thank you in advance.

Please forward me a copy of you reply as I can't always check the

 Wed, 16 Jun 2004 07:53:58 GMT   
 KSH for loop question
In article <>,

Since the directories don't have subdirectories, why do you need the -R
option on this?

Since awk has a built-in regular expression matcher, you don't need the
grep.  Use:

ls -l | awk '/^d/ {print $9}'

You need the /^d/ like above so that this will only contain directories.

for i in $DIRLIST
  echo $i `ls -l $i | wc -l`

Barry Margolin,
Genuity, Woburn, MA
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 Wed, 16 Jun 2004 08:28:57 GMT   
 KSH for loop question
In article <>,

    echo dir*/. | wc -w

    echo dir*/* | wc -w

    Breaking out the number of files in each directory requires a
    'for' loop, see below.

    DIRNAMES=`echo dir*/. | sed 's;/\.;;g'`

    du -k -s $DIRNAMES

    for i in $DIRNAMES
        echo "$i\t`echo $i/*|wc -w`"
    The shell puts all matches in place of a wildcard so you can
    generate the list 'dir1/. dir2/. dir3/. [...]' with the
    single pattern of dir*/. without need of the 'ls' unless the
    names have special characters like spaces in them or their
    are too many to be passed on the command line(usually in the

Unix has security which has been tested by conniving, unscrupulous
    college students over generations.

 Thu, 17 Jun 2004 07:00:22 GMT   
   [ 3 post ] 

Similar Threads

1. ksh For Loop Question

2. ksh question about "for" loops

3. Looping question using ksh?

4. ksh -c for loops ?????

5. If loop in Ksh

6. Loop variables in KSH - Linux / HPUX difference

7. Hekp with a while loop - Ksh

8. ksh while loop with multple reads

9. While loop in ksh

10. ksh and variables set inside of while loop

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software