It is currently Thu, 24 Apr 2014 15:44:38 GMT



 
Author Message
 KSH for loop question
Hi:

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:
#!/usr/bin/ksh
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
like
DIRLIST=`ls -l | awk '{print $9}'`
for i in $DIRLIST; do cd $i (and echo $i and output of "ls -l $i | wc
-l")
Something like that.

Any help is greatly apppreciated.

Thank you in advance.
-Berardo
beste...@mathlab.sunysb.edu

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



 Wed, 16 Jun 2004 07:53:58 GMT   
 KSH for loop question
In article <8f0b1c83.0112281553.44c9...@posting.google.com>,

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
do
  echo $i `ls -l $i | wc -l`
done

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
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 <8f0b1c83.0112281553.44c9...@posting.google.com>,

    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
    do
        echo "$i\t`echo $i/*|wc -w`"
    done
    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
    thousands).

--
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