Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is the command I am using to list some files:

find . -name \*.extract.sys -size +1000000c -exec ls -lrt {} \;
-rw-r--r--   1 qa1wrk15   test       1265190 Sep 29 01:14 ./var/can/projs/ar/rep/extract/Sep/29/ar.ARAB-PI_7.20110929.extract.sys
-rw-r--r--   1 qa1wrk15   test       1345554 Sep 29 01:14 ./var/can/projs/ar/rep/extract/Sep/29/ar.ARAB-PI_2.20110929.extract.sys
-rw-r--r--   1 qa1wrk15   test       1370532 Sep 29 01:14 ./var/can/projs/ar/rep/extract/Sep/29/ar.ARAB-PI_3.20110929.extract.sys
-rw-r--r--   1 qa1wrk15   test       1399854 Sep 29 01:14 ./var/can/projs/ar/rep/extract/Sep/29/ar.ARAB-PI_8.20110929.extract.sys

and so on.

Now I want to calculate the total size of these files by summing up the 5th column. I thought of using awk, to do this so I tested the following in a particular directory

>ls -lrt | awk `{ print $1 }`
ksh: syntax error at line 1 : `{' unmatched

I don't understand what is the problem, why this syntax error.

I am thinking to try

ls -lrt | awk `BEGIN {total = 0} {for(i=0;i<NR;i++){total+=$5}} END {printf "%d",total} 

this also, but a simple awk script is not working.

Please suggest or correct me if I am wrong, or if there is a workaround for this.

share|improve this question
up vote 16 down vote accepted

First of all, you should use straight single quotes ('), not the inclined ones (`).

The awk inline script could be as follow:

ls -lrt | awk '{ total += $5 }; END { print total }'

so, no need to initialize total (awk initializes it to zero), and no need to loop, awk already executes the script on every line of input.

share|improve this answer
thanks a lot, @enzofib – munish Sep 29 '11 at 7:07
In summary, the one-liner would be this: find . -name \*.extract.sys -size +1000000c -exec ls -lrt {} \; | awk '{ total += $5 }; END { print total }' – helt Apr 27 at 15:35

@enzotib has already pointed out what your syntax error is - I'm going to go off on a little tangent.

Summing a column of numbers is one of those things that keeps popping up. I've ended up with this shell function:

    awk "{sum+=\$$1} END {print sum}"

With this, your solution becomes:

ls -lrt | sumcol 5

That will sum the numbers in column 5 and print the value.

share|improve this answer
In effect, I was wondering why there is not a standard utility for that. – enzotib Sep 29 '11 at 9:34
Goes right into my .aliases file. – Konrad Rudolph Sep 29 '11 at 12:04

Here is another way to do this by using du:

find . -name \*.extract.sys -size +1000000c -print0 | du -c --files0-from=- | awk 'END{print $1}'
share|improve this answer

In case HP-UX find also has -printf option, that would be useful to compose a formula for bc to calculate:

( find . -name \*.extract.sys -size +1000000c -printf '%s+'; echo 0 ) | bc

If the formula gets too big, it can be calculated step-by-step:

( find . -name \*.extract.sys -size +1000000c -printf 's+=%s\n'; echo s ) | bc
share|improve this answer

With GNU find:

find -name \*.extract.sys -size +1000000c -printf %s\\n|jq -s add

With GNU stat:

find . -name \*.extract.sys -size +1000000c -exec stat -c%s {} +|jq -s add

With BSD stat:

find . -name \*.extract.sys -size +1000000c -exec stat -f%z {} +|jq -s add
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.