What's wrong with this Bourne shell script?

D'Arcy J.M. Cain darcy at druid.uucp
Thu Aug 9 05:46:18 AEST 1990


In article <834 at hls0.hls.oz> george at hls0.hls.oz (George Turczynski) writes:
>In article <1990Aug3.193231.3166 at silma.com>, aab at silma.com (Andy Burgess) writes:
>> While you're at it, is there a better way to get the total bytes
>> of the files in a directory?
>You could try something like this:
>ls -ld * | awk '{ sum+= $4 } END { print sum }'

Here is a script I use to check directory usage:

------------------------ Start of script ----------------------------
#!
# Checks the space used by a directory
# Written by D'Arcy J.M. Cain

# default to current directory else from command line
if [ "$1" = "" ]
then
	directory=`pwd`
else
	directory=$1
	shift
fi

while true
do
	if [ "$directory" = "" ]
	then
		exit 0
	fi

	echo "$directory \c"
	ls -iRl $directory | awk '{ if (NF > 5) print $0 }' | sort | awk '{
		if ($1 != last_inode) size = size + $6
		last_inode = $1
		}
		END { print size }'

	if [ "$1" = "" ]
	then
		exit 0
	fi

	shift
	directory=$1
done
---------------------- End of script ----------------------

The difference is that subdirectories are included and links are only
counted once.  Also there is a little user friendly wrapping.  HTH.

-- 
D'Arcy J.M. Cain (darcy at druid)     |
D'Arcy Cain Consulting             |   MS-DOS:  The Andrew Dice Clay
West Hill, Ontario, Canada         |   of operating systems.
+ 416 281 6094                     |



More information about the Comp.unix.questions mailing list