• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 505
  • Last Modified:

Sum Size Column From ls -la Command

Partial listing of ls -la command.  I need a simple script that will sum up the size column .


-rw-r-----   1 oracle   oracle   31652864 Sep 19 13:21 1_124188_665266938.arc
-rw-r-----   1 oracle   oracle   44494848 Sep 19 13:36 1_124189_665266938.arc
-rw-r-----   1 oracle   oracle   32430080 Sep 19 13:51 1_124190_665266938.arc
-rw-r-----   1 oracle   oracle   92637184 Sep 19 14:06 1_124191_665266938.arc
-rw-r-----   1 oracle   oracle   92578304 Sep 19 14:13 1_124192_665266938.arc
-rw-r-----   1 oracle   oracle   60880384 Sep 19 14:28 1_124193_665266938.arc
-rw-r-----   1 oracle   oracle   37847040 Sep 19 14:43 1_124194_665266938.arc
-rw-r-----   1 oracle   oracle   46640640 Sep 19 14:58 1_124195_665266938.arc
-rw-r-----   1 oracle   oracle   73367552 Sep 19 15:13 1_124196_665266938.arc
-rw-r-----   1 oracle   oracle   95905280 Sep 19 15:22 1_124197_665266938.arc
0
xoxomos
Asked:
xoxomos
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
Hugh FraserConsultantCommented:
If you're open to another command, "du -bc *" will produce a grand total. The "C" generates the total, and the "b" converts it from blocks to actual number of bytes similar to what the "ls -l" command produces.
0
 
xoxomosAuthor Commented:
Yes that's true.  Actually it was ls -la > somefile.  Once into somefile i eliminated some of the rows and I wanted to get the sum of that.  
Another alternative would be if i could do something like the du but only include those with the latest date(today)???
0
 
sakmanCommented:
There are many ways to go about this, including using the "du" command as hfraser mentioned.

If you want to continue putting your file listing into a file to process, you could do something like this:
$ ls -l /some/dir | grep 'Sep 19' >somefile
$ total=0
$ for size in `cat somefile | awk '{print $5}'`
> do
> total=`expr $total + $size`
> done
$ echo $total
608434176
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Hugh FraserConsultantCommented:
A simple awk script will tdo it for you.

{
  sum = sum + $5}
END {
 print sum
}

Just use cat filename | awk script.awk
0
 
xoxomosAuthor Commented:
Mil gracias everyone!!!
0
 
TintinCommented:
Much easer and quicker to do
ls -la | awk '{t+=$5} END {print t}'

Open in new window

0
 
xoxomosAuthor Commented:
sakman was most complete because this solution included ability to select a specific date :-)
Thanks
0
 
sakmanCommented:
IMHO, Tintin's solution is the most elegant - just add a grep for the date (depending on the date format of your system):

ls -la | grep 'Sep 19'  | awk '{t+=$5} END {print t}'

Open in new window

0
 
xoxomosAuthor Commented:
Yes.  Tintin always comes up with the most sexy solutions :-)  A bit late this time though :-(
0
 
TintinCommented:
With one less process :-)
ls -la | awk '/Sep 19/ {t+=$5} END {print t}'

Open in new window

0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now