Sum Size Column From ls -la Command

Posted on 2011-09-19
Last Modified: 2012-05-12
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
Question by:xoxomos
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
LVL 12

Expert Comment

ID: 36564055
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.

Author Comment

ID: 36564133
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)???

Assisted Solution

sakman earned 250 total points
ID: 36564243
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Accepted Solution

hfraser earned 250 total points
ID: 36564296
A simple awk script will tdo it for you.

  sum = sum + $5}
 print sum

Just use cat filename | awk script.awk

Author Comment

ID: 36564346
Mil gracias everyone!!!
LVL 48

Expert Comment

ID: 36564388
Much easer and quicker to do
ls -la | awk '{t+=$5} END {print t}'

Open in new window


Author Comment

ID: 36584067
sakman was most complete because this solution included ability to select a specific date :-)

Expert Comment

ID: 36584096
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


Author Comment

ID: 36584125
Yes.  Tintin always comes up with the most sexy solutions :-)  A bit late this time though :-(
LVL 48

Expert Comment

ID: 36589952
With one less process :-)
ls -la | awk '/Sep 19/ {t+=$5} END {print t}'

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
FreeBSD on EC2 FreeBSD ( is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question