#!/bin/bash # # CIS 130 Lab 5 - tally # # Rich Simms # Spring 2007 # # This script tallies the different entries in the specified directory # # usage: tally [-d] path # # Initialize usage="lookup [-d] user" # Process debug option (-d) while getopts ":d" opt; do case $opt in d ) debug="y" echo "** Debug tracing enabled" ;; \? ) echo "-- Usage: " $usage >&2 exit 1 esac done # Shift out any options if [ $OPTIND -ge 1 ]; then shift $(($OPTIND - 1)); fi directory=$1 # If no directory specified use current one if [ "$directory" = "" ]; then directory="$(pwd)" fi # Validate it is a directory if [ -d $directory ]; then : # null command else echo "-- $directory is not a valid directory" exit 1 fi if [ "$debug" = "y" ]; then echo "** trace: directory = " $directory fi # Start the tally ls -l $directory > tallydata dtype=0; rtype=0; ltype=0; otype=0; while read inputline do type="$(echo $inputline | cut -c1-1)" if [ "$debug" = "y" ]; then echo "** trace: type=$type" fi case "$type" in [d] ) (( dtype++ )) ;; [-] ) (( rtype++ )) ;; [l] ) (( ltype++ )) ;; [t] ) : ;; #ignore word total * ) (( otype++ )) ;; esac done < tallydata echo "Tally (using loop) for directory: " $directory echo " #Regular files: " $rtype echo " #Directory files: " $dtype echo " #Symbolic links: " $ltype echo " #Other types: " $otype # Just for kicks lets try another way rtype=$(ls -l $directory | grep ^-.* | wc -l) dtype=$(ls -l $directory | grep ^d.* | wc -l) ltype=$(ls -l $directory | grep ^l.* | wc -l) total=$(ls -l $directory | wc -l) ((total--)) otype=$(($total - $rtype - $dtype - $ltype)) echo "Tally (using grep) for directory: "$directory echo " #Regular files: " $rtype echo " #Directory files: " $dtype echo " #Symbolic links: " $ltype echo " #Other types: " $otype exit 0 /home/cis130/simmsric/lab6 $ ./tally -d . ** Debug tracing enabled ** trace: directory = . ** trace: type=t ** trace: type=- ** trace: type=- Tally (using loop) for directory: . #Regular files: 2 #Directory files: 0 #Symbolic links: 0 #Other types: 0 Tally (using grep) for directory: . #Regular files: 2 #Directory files: 0 #Symbolic links: 0 #Other types: 0 /home/cis130/simmsric/lab6 $ ls -l total 8 -rwxr-xr-x 1 simmsric cis130 1906 Mar 26 17:24 tally -rw-r--r-- 1 simmsric cis130 120 Mar 26 17:29 tallydata /home/cis130/simmsric/lab6 $ ls -l .. total 48 drwxr-xr-x 2 simmsric cis130 4096 Feb 15 19:18 backup drwxr-xr-x 2 simmsric cis130 4096 Feb 16 15:21 bin drwxr-xr-x 2 simmsric cis130 4096 Mar 1 20:57 class -rw-r--r-- 1 simmsric cis130 1266 Feb 25 09:45 frodo-sim.txt drwxrwx--- 2 simmsric staff 4096 Feb 19 19:20 lab1 drwxrwx--- 2 simmsric staff 4096 Feb 22 17:32 lab2 drwxr-xr-x 2 simmsric cis130 4096 Feb 25 09:51 lab3 drwxr-xr-x 2 simmsric cis130 4096 Mar 17 13:30 lab4 drwxr-xr-x 2 simmsric cis130 4096 Mar 17 18:09 lab5 drwxr-xr-x 2 simmsric cis130 4096 Mar 26 17:25 lab6 -rwxr-xr-x 1 simmsric cis130 77 Mar 15 18:38 mysetup -rwxr-xr-x 1 simmsric cis130 111 Mar 15 19:01 yoursetup /home/cis130/simmsric/lab6 $ ./tally -d .. ** Debug tracing enabled ** trace: directory = .. ** trace: type=t ** trace: type=d ** trace: type=d ** trace: type=d ** trace: type=- ** trace: type=d ** trace: type=d ** trace: type=d ** trace: type=d ** trace: type=d ** trace: type=d ** trace: type=- ** trace: type=- Tally (using loop) for directory: .. #Regular files: 3 #Directory files: 9 #Symbolic links: 0 #Other types: 0 Tally (using grep) for directory: .. #Regular files: 3 #Directory files: 9 #Symbolic links: 0 #Other types: 0 /home/cis130/simmsric/lab6 $ ls -l /etc total 3176 drwxr-xr-x 2 root root 4096 Jan 17 2006 4Suite -rw-r--r-- 1 root root 15289 Mar 23 2005 a2ps.cfg -rw-r--r-- 1 root root 2562 Mar 23 2005 a2ps-site.cfg drwxr-xr-x 4 root root 4096 Mar 16 2005 acpi -rw-r--r-- 1 root root 48 Feb 2 22:12 adjtime drwxr-xr-x 4 root root 4096 Mar 3 2005 alchemist -rw-r--r-- 1 root root 1512 Apr 25 2005 aliases -rw-r----- 1 root smmsp 12288 Feb 2 22:13 aliases.db drwxr-xr-x 4 root root 4096 Jan 17 2006 alsa drwxr-xr-x 2 root root 4096 Jan 17 2006 alternatives -rw-r--r-- 1 root root 329 Mar 16 2005 anacrontab -rw-r--r-- 1 root root 395 Jan 12 2005 ant.conf drwxr-xr-x 2 root root 4096 May 23 2005 ant.d -rw------- 1 root root 1 Apr 8 2005 at.deny -rw-r--r-- 1 root root 343 Apr 25 2005 auto.master -rw-r--r-- 1 root root 581 Apr 25 2005 auto.misc -rwxr-xr-x 1 root root 1272 Apr 25 2005 auto.net -rwxr-xr-x 1 root root 538 Apr 25 2005 auto.smb -rw-r--r-- 1 root root 1171 Nov 7 17:50 bashrc -rw-r--r-- 1 root root 711 Feb 2 22:13 blkid.tab -rw-r--r-- 1 root root 711 Feb 2 22:13 blkid.tab.old drwxr-xr-x 2 root root 4096 Jan 17 2006 bonobo-activation -rw-r--r-- 1 root root 976 Apr 6 2005 cdrecord.conf -rw-r--r-- 1 root root 23 Feb 12 2006 cis130.accounts.02-12-2006 -rw-r--r-- 1 root root 23 Feb 15 19:08 cis130.accounts.02-15-2007 -rw-r--r-- 1 root root 463 May 9 2005 cpuspeed.conf drwxr-xr-x 2 root root 4096 Apr 14 2005 cron.d drwxr-xr-x 2 root root 4096 Jan 17 2006 cron.daily -rw-r--r-- 1 root root 0 Jan 17 2006 cron.deny drwxr-xr-x 2 root root 4096 Sep 20 2004 cron.hourly drwxr-xr-x 2 root root 4096 Jan 17 2006 cron.monthly -rw-r--r-- 1 root root 255 Sep 20 2004 crontab drwxr-xr-x 2 root root 4096 Jan 17 2006 cron.weekly -rw-r--r-- 1 root root 665 May 19 2005 csh.cshrc -rw-r--r-- 1 root root 535 Nov 18 2004 csh.login drwxrwxr-x 5 root sys 4096 Dec 12 16:44 cups drwxr-xr-x 3 root root 4096 Jan 17 2006 dbus-1 drwxr-xr-x 2 root root 4096 Jan 17 2006 default drwxr-xr-x 9 root root 4096 Jan 17 2006 dev.d -rw-r--r-- 1 root root 0 Dec 7 2005 dhclient-eth0.conf -rw-r--r-- 1 root root 2518 May 25 2005 DIR_COLORS -rw-r--r-- 1 root root 2434 May 25 2005 DIR_COLORS.xterm -rw-r--r-- 1 root root 20379 Apr 22 2005 dovecot.conf -rw-rw-r-- 1 root disk 0 May 11 2005 dumpdates -rw-r--r-- 1 root root 4975 Mar 8 2005 enscript.cfg -rw-r--r-- 1 root root 0 May 19 2005 environment -rw-r--r-- 1 root root 153 Jun 1 2005 esd.conf -rw-r--r-- 1 root root 0 Jan 12 2000 exports -rw-r--r-- 1 root root 22060 Mar 4 2005 fb.modes -rw-r--r-- 1 root root 1246 May 3 2005 fdprm -rw-r--r-- 1 root root 31 Jun 3 2005 fedora-release -rw-r--r-- 1 root root 51 Mar 12 2002 filesystems drwxr-xr-x 2 root root 4096 Jan 17 2006 firmware drwxr-xr-x 2 root root 4096 Jan 17 2006 fonts drwxr-xr-x 2 root root 4096 Jan 17 2006 foomatic -rw-r--r-- 1 root root 777 Feb 2 22:13 fstab -rw-r--r-- 1 root root 657 Dec 7 2005 fstab.rpmsave drwxr-xr-x 2 root root 4096 Jan 17 2006 gaim drwxr-xr-x 7 root root 4096 May 17 2005 gconf drwxr-xr-x 2 root root 4096 Mar 10 2005 ghostscript drwxr-xr-x 3 root root 4096 Apr 29 2005 gnome-vfs-2.0 -rw-r--r-- 1 root root 10793 Jan 27 2005 gnome-vfs-mime-magic -rw-r--r-- 1 root root 1756 Mar 4 2005 gpm-root.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 gre.d -rw-r--r-- 1 root root 1344 Feb 19 18:02 group -rw------- 1 root root 1335 Feb 18 18:56 group- lrwxrwxrwx 1 root root 22 Mar 23 2005 grub.conf -> ../boot/grub/grub.conf -r-------- 1 root root 973 Feb 19 18:02 gshadow -rw------- 1 root root 964 Feb 18 18:56 gshadow- -rw-r--r-- 1 root root 804 May 26 2005 gssapi_mech.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 gtk drwxr-xr-x 3 root root 4096 Jan 17 2006 gtk-2.0 drwxr-xr-x 3 root root 4096 Jan 17 2006 hal -rw-r--r-- 1 root root 17 Jul 23 2000 host.conf -rw-r--r-- 1 root root 170 Feb 28 2006 hosts -rw-r--r-- 1 root root 161 Jan 12 2000 hosts.allow -rw-r--r-- 1 root root 347 Jan 12 2000 hosts.deny drwxr-xr-x 4 root root 4096 Jan 17 2006 hotplug drwxr-xr-x 3 root root 4096 May 23 2005 hotplug.d drwxr-xr-x 2 root root 4096 Jan 17 2006 howl drwxr-xr-x 4 root root 4096 Jan 17 2006 httpd -rw-r--r-- 1 root root 177 May 26 2005 idmapd.conf -rw-r--r-- 1 root root 3376 Apr 9 2005 im_palette.pal -rw-r--r-- 1 root root 920 Apr 9 2005 im_palette-small.pal -rw-r--r-- 1 root root 224 Apr 9 2005 im_palette-tiny.pal -rw-r--r-- 1 root root 5464 Apr 9 2005 imrc lrwxrwxrwx 1 root root 11 Jan 17 2006 init.d -> rc.d/init.d -rw-r--r-- 1 root root 658 May 25 2005 initlog.conf -rw-r--r-- 1 root root 1667 Jan 17 2006 inittab -rw-r--r-- 1 root root 1663 May 25 2005 inittab.rpmnew -rw-r--r-- 1 root root 1667 Jul 19 2005 inittab.rpmsave -rw-r--r-- 1 root root 758 Sep 22 2004 inputrc drwxr-xr-x 2 root root 4096 Jan 17 2006 iproute2 drwxr-xr-x 2 root root 4096 Jan 17 2006 isdn -rw-r--r-- 1 root root 51 Jun 3 2005 issue -rw-r--r-- 1 root root 50 Jun 3 2005 issue.net -rw-r--r-- 1 root root 50 Jul 17 2005 issue.rpmsave drwxr-xr-x 3 root root 4096 Jan 17 2006 java -rw-r--r-- 1 root root 28686 Apr 30 2005 jwhois.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 kermit -rw-r--r-- 1 root root 657 Mar 28 2005 krb5.conf -rw-r--r-- 1 root root 2281 Mar 23 2005 krb.conf -rw-r--r-- 1 root root 1296 Mar 16 2005 krb.realms -rw-r--r-- 1 root root 6952 Mar 23 2005 ldap.conf -rw-r--r-- 1 root root 8625 May 20 2005 ldap.conf.rpmnew -rw-r--r-- 1 root root 53485 Jan 17 2006 ld.so.cache -rw-r--r-- 1 root root 63 Mar 23 2005 ld.so.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 ld.so.conf.d -rw-r--r-- 1 root root 3243 Apr 25 2005 lftp.conf -rw-r--r-- 1 root root 2374 Apr 30 2005 libuser.conf -rw------- 1 root root 394 Jan 17 2006 lilo.conf.anaconda -rw-r--r-- 1 root root 844 Mar 23 2005 localtime drwxr-xr-x 5 root root 4096 Jan 17 2006 log.d -rw-r--r-- 1 root root 1180 May 23 2005 login.defs -rw-r--r-- 1 root root 596 Mar 31 2005 logrotate.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 logrotate.d -rw-r--r-- 1 root root 10815 Nov 9 2004 ltrace.conf drwxr-xr-x 4 root root 4096 Jan 17 2006 lvm drwxr-xr-x 3 root root 4096 Jan 17 2006 mail -rw-r--r-- 1 root root 293 Mar 7 2005 mailcap -rw-r--r-- 1 root root 112 Mar 8 2005 mail.rc drwxr-xr-x 2 root root 4096 Jan 17 2006 makedev.d -rw-r--r-- 1 root root 4506 Apr 8 2005 man.config -rw-r--r-- 1 jimg root 754 Jul 17 2005 message -rw-r--r-- 1 root root 13085 Mar 7 2005 mime.types -rw-r--r-- 1 root root 1110 Mar 7 2005 minicom.users -rw-r--r-- 1 root root 127 Jun 18 2005 modprobe.conf -rw-r--r-- 1 root root 95 Mar 23 2005 modprobe.conf~ -rw-r--r-- 1 root root 5717 Apr 25 2005 modprobe.conf.dist -rw-r--r-- 1 root root 109 Jul 19 2006 motd -rw-r--r-- 1 root root 447 Feb 2 22:13 mtab -rw-r--r-- 1 root root 1983 Mar 21 2005 mtools.conf -rw-r--r-- 1 root root 92812 Mar 7 2005 Muttrc -rw-r--r-- 1 root root 0 Mar 7 2005 Muttrc.local -rw-r--r-- 1 root root 346 Apr 8 2005 my.cnf lrwxrwxrwx 1 root root 32 Jan 17 2006 named.conf -> /var/named/chroot/etc/named.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 netplug drwxr-xr-x 2 root root 4096 Jan 17 2006 netplug.d -rw-r--r-- 1 root root 1670 May 30 2005 nscd.conf -rw-r--r-- 1 root root 1686 Mar 23 2005 nsswitch.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 ntp -rw-r--r-- 1 root root 2434 Apr 14 2005 ntp.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 oaf drwxr-xr-x 3 root root 4096 Jan 17 2006 openldap drwxr-xr-x 2 root root 4096 May 22 2005 opt drwxr-xr-x 2 root root 4096 Jan 17 2006 pam.d -rw-r--r-- 1 root root 10 Mar 23 2005 pam_smb.conf drwxr-xr-x 3 root root 4096 Jan 17 2006 pango -rw-r--r-- 1 root root 6682 Mar 5 12:58 passwd -rw------- 1 root root 6657 Feb 23 15:33 passwd- -rw-r--r-- 1 root root 5941 Feb 18 23:51 passwd.OLD -rw-r--r-- 1 root root 1362 Mar 16 2005 pbm2ppa.conf drwxr-xr-x 3 root root 4096 Jan 17 2006 pcmcia -rw-r--r-- 1 root root 2888 Apr 22 2005 pinforc drwxr-xr-x 6 root root 4096 Jan 17 2006 pki -rw-r--r-- 1 root root 6300 Mar 16 2005 pnm2ppa.conf drwxr-xr-x 3 root root 4096 Jan 17 2006 ppp -rw-r--r-- 1 root root 249891 Mar 24 04:02 prelink.cache -rw-r--r-- 1 root root 821 Oct 1 2004 prelink.conf -rw-r--r-- 1 root root 227 Mar 25 04:02 printcap -rw-r--r-- 1 root root 916 May 2 2006 profile drwxr-xr-x 2 root root 4096 Jan 17 2006 profile.d -rw-r--r-- 1 root root 5834 Jul 7 2001 protocols -rw-r--r-- 1 root root 134 May 2 2005 pwdb.conf -rw-r--r-- 1 root root 220 Apr 11 2005 quotagrpadmins -rw-r--r-- 1 root root 257 Apr 11 2005 quotatab lrwxrwxrwx 1 root root 7 Jan 17 2006 rc -> rc.d/rc lrwxrwxrwx 1 root root 10 Jan 17 2006 rc0.d -> rc.d/rc0.d lrwxrwxrwx 1 root root 10 Jan 17 2006 rc1.d -> rc.d/rc1.d lrwxrwxrwx 1 root root 10 Jan 17 2006 rc2.d -> rc.d/rc2.d lrwxrwxrwx 1 root root 10 Jan 17 2006 rc3.d -> rc.d/rc3.d lrwxrwxrwx 1 root root 10 Jan 17 2006 rc4.d -> rc.d/rc4.d lrwxrwxrwx 1 root root 10 Jan 17 2006 rc5.d -> rc.d/rc5.d lrwxrwxrwx 1 root root 10 Jan 17 2006 rc6.d -> rc.d/rc6.d drwxr-xr-x 10 root root 4096 Jan 31 2006 rc.d lrwxrwxrwx 1 root root 13 Jan 17 2006 rc.local -> rc.d/rc.local lrwxrwxrwx 1 root root 15 Jan 17 2006 rc.sysinit -> rc.d/rc.sysinit -rw-r--r-- 1 root root 27187 May 18 2005 readahead.early.files -rw-r--r-- 1 root root 75177 May 18 2005 readahead.files lrwxrwxrwx 1 root root 14 Jan 17 2006 redhat-release -> fedora-release -rw-r--r-- 1 root root 70 Jul 28 2005 resolv.conf -rw-r--r-- 1 root root 0 Mar 23 2005 resolv.conf.predhclient drwxr-xr-x 3 root root 4096 Jan 17 2006 rhgb lrwxrwxrwx 1 root root 11 Jan 17 2006 rmt -> ../sbin/rmt -rw-r----- 1 root named 1117 May 17 2005 rndc.conf lrwxrwxrwx 1 root root 30 Jan 17 2006 rndc.key -> /var/named/chroot/etc/rndc.key -rw-r--r-- 1 root root 1615 May 30 2005 rpc drwxr-xr-x 2 root root 4096 Jan 17 2006 rpm drwxr-xr-x 2 root root 4096 Jan 17 2006 samba -rw-r--r-- 1 root root 7113 Apr 28 2004 screenrc -rw-r--r-- 1 root root 103 Mar 17 2005 scrollkeeper.conf -rw-r--r-- 1 root root 1444 May 20 2005 scsi_id.config -rw------- 1 root root 122 Feb 17 2003 securetty drwxr-xr-x 4 root root 4096 Jan 17 2006 security drwxr-xr-x 4 root root 4096 Jan 17 2006 selinux -rw-r--r-- 1 root root 65682 Mar 2 2005 sensors.conf -rw-r--r-- 1 root root 20385 Nov 5 2004 services -rw-r--r-- 1 root root 216 May 19 2005 sestatus.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 setuptool.d drwxr-xr-x 2 root root 4096 Jan 17 2006 sgml -r-------- 1 root root 6079 Mar 15 19:49 shadow -rw------- 1 root root 5697 Feb 23 15:33 shadow- -rw-r--r-- 1 root root 80 Mar 23 2005 shells drwxr-xr-x 2 root root 4096 Jan 17 2006 skel -rw-r--r-- 1 root root 21851 Mar 7 2005 slrn.rc -rw-r--r-- 1 root root 42 Jan 17 2006 smartd.conf drwxr-xr-x 2 root root 4096 May 6 2005 smrsh drwxr-xr-x 2 root root 4096 Jan 17 2006 snmp drwxr-xr-x 3 root root 4096 May 27 2005 sound drwxr-xr-x 2 root root 4096 Jan 17 2006 ssh drwxr-xr-x 2 root root 4096 Jan 17 2006 stunnel -r--r----- 1 root root 579 Oct 25 2005 sudoers -r--r----- 1 root root 608 Apr 12 2005 sudoers.rpmnew drwxr-xr-x 8 root root 4096 Feb 26 2006 sysconfig -rw-r--r-- 1 root root 670 May 25 2005 sysctl.conf -rw-r--r-- 1 root root 694 May 19 2005 syslog.conf -rw-r--r-- 1 root root 799248 Mar 16 2005 termcap drwxr-xr-x 6 root root 4096 Jan 17 2006 udev -rw-r--r-- 1 root root 257 Apr 16 2005 updatedb.conf lrwxrwxrwx 1 root root 34 Jan 17 2006 vfontcap -> ../usr/share/VFlib/2.25.6/vfontcap drwxr-xr-x 3 root root 4096 Mar 23 2005 vfs -rw-r--r-- 1 root root 1438 Apr 26 2005 vimrc -rw-r--r-- 1 root root 1438 Apr 26 2005 vimrc.rpmnew drwxr-xr-x 6 root root 4096 Feb 2 23:03 vmware drwxr-xr-x 6 root root 4096 Feb 5 2006 vmware.00 drwxr-xr-x 2 root root 4096 Feb 24 20:55 vsftpd -rw-r--r-- 1 root root 2657 Apr 11 2005 warnquota.conf -rw-r--r-- 1 root root 4002 Mar 2 2005 wgetrc -rw-r--r-- 1 root root 0 Mar 3 2005 wvdial.conf drwxr-xr-x 16 root root 4096 Jan 17 2006 X11 drwxr-xr-x 3 root root 4096 Apr 14 2005 xdg -rw-r--r-- 1 root root 289 Mar 8 2005 xinetd.conf drwxr-xr-x 2 root root 4096 Nov 25 19:52 xinetd.d drwxr-xr-x 2 root root 4096 Sep 22 2004 xml -rw-r--r-- 1 root root 585 Jan 24 2005 yp.conf -rw-r--r-- 1 root root 1757 Apr 11 2005 ypserv.conf drwxr-xr-x 2 root root 4096 Jan 17 2006 yum -rw-r--r-- 1 root root 253 May 25 2005 yum.conf drwxr-xr-x 2 root root 4096 May 25 2005 yum.repos.d /home/cis130/simmsric/lab6 $ ./tally /etc Tally (using loop) for directory: /etc #Regular files: 143 #Directory files: 79 #Symbolic links: 17 #Other types: 0 Tally (using grep) for directory: /etc #Regular files: 143 #Directory files: 79 #Symbolic links: 17 #Other types: 0 /home/cis130/simmsric/lab6 $