Shell script to calc sum of bytes used by files | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Shell script to calc sum of bytes used by files

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-18-2006
kecannon kecannon is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 21 January 2006, 4:05 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Shell script to calc sum of bytes used by files

I'm looking to create a Korn Shell script that, if given a directory as an arg, will calc bytes used by all files in the given directory and display that info. If no command line arg is given the program is to calc and display the bytes used by all the files in the pwd.

Example output:

/export/home/me> ./filesizes /export/home
6325832


Thanks...
Sponsored Links
    #2  
Old 01-18-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
sum the 5th column of ls -l.


Code:
# !/usr/bin/ksh

if [ $# -eq 0 ]
then
dir="."
else
dir=$1
fi

tot=`ls -l $dir | awk -F" " 'BEGIN {sum=0} {sum+=$5} END {print sum}'`
echo "$tot bytes"

exit 0

Sponsored Links
    #3  
Old 01-18-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
 
Join Date: Dec 2005
Last Activity: 22 August 2008, 8:28 AM EDT
Location: London
Posts: 222
Thanks: 0
Thanked 0 Times in 0 Posts
#!/usr/bin/ksh

ls -l $1 | awk 'BEGIN { FS=" "; sm=0; } { sm = sm + $5 } END { print "Total bytes : "sm; }'



this will take care of what you have asked...
if $1 contains value, then it will calculate for givent directory
if $0 contains nothing, then it will calculate for current directory
    #4  
Old 01-18-2006
kecannon kecannon is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 21 January 2006, 4:05 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you both. Very much appreciated.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copying 1024 bytes data in 3-bytes chunk royalibrahim Programming 3 04-13-2011 03:32 PM
Shell script to copy a log file if it exceeds 5000000 bytes lookinginfo Shell Programming and Scripting 6 01-04-2011 12:25 AM
shell script to convert file_size from bytes to megabytes a1_win Shell Programming and Scripting 3 08-06-2009 05:33 PM
Files with zero bytes preethgideon UNIX for Dummies Questions & Answers 1 04-24-2009 06:22 PM
Remove first N bytes and last N bytes from a binary file on AIX. naveendronavall Shell Programming and Scripting 1 05-24-2008 12:06 PM



All times are GMT -4. The time now is 11:18 AM.