1753821 Members
9156 Online
108805 Solutions
New Discussion юеВ

Awk problem - addition?

 
SOLVED
Go to solution
Vassilios
Frequent Advisor

Awk problem - addition?

Hello,
Would someone here know how to use awk to do an addition?

Here's what I'm trying to do:

if you do an ls -l, it prints out direcctory/file sizes. Right? So, let's say the column in which the directory falls is the 9th? I forget.. but its around the 9th,

so, I now want to add the TOTAL file sizes/sub directory sizes in a directory, that may contain 200 files or sub folders.

ls -l | swk '{print $9}' |

Thanks

11 REPLIES 11
Deepak Kr
Respected Contributor
Solution

Re: Awk problem - addition?

Hi,

Instead you can choose easier way !!

Use du -sk to take out size of directory including all sub dirs and files in it

#du -sk /tmp/dir
size in KB

MB=KB/1000
"There is always some scope for improvement"
James R. Ferguson
Acclaimed Contributor

Re: Awk problem - addition?

Hi:

# ls -l | awk '/^total/ {next};{SUM+=$5};END{printf "%10d\n",SUM}'

...note that this skips the "total" line returned by 'ls -l'.

Regards!

...JRF...
TTr
Honored Contributor

Re: Awk problem - addition?

You may want to include a grep after the ls command to capture only the files and exclude directories soft links and other special files. Such as
ls -l |grep ^- | awk...
Note that in the "ls -l" listing for a subdirectory line the size number does NOT represent the contents of the subdirectory but rather its file table. You may or may not want to exclude these. If you want to sum up the contents of subrirectories you have to use the df commnad as noted above.
Deepak Kr
Respected Contributor

Re: Awk problem - addition?

Also:

To find
a) The total number of files in a directory
b) The total filesize

#cd /dir
#ls -l | awk '$6{b+=$5;a+=1}END{print a,b}'


"There is always some scope for improvement"
James R. Ferguson
Acclaimed Contributor

Re: Awk problem - addition?

Hi (again):

I agree with TTr insofar as you probably want to exclude the space used for directories and for symbolic links. You don't need to add another process, namely 'grep', though since 'awk' easily does this too:

# ls -l | awk '!/^-/ {next};{SUM+=$5};END{printf "%10d\n",SUM}'

Now, if you truly want the size of all *files* in all of the subdirectories in your 'ls' path, you can add the '-R' switch to 'ls' to do:

# ls -lR | awk '!/^-/ {next};{SUM+=$5};END{printf "%10d\n",SUM}'

Regards!

...JRF...

Regards!

...JRF...
Vassilios
Frequent Advisor

Re: Awk problem - addition?

Hi Thanks a lot. That's really good. But I'm just curious, what's wrong with this?

ls -l | awk '{sum += $5};END{printf "%d\n", sum"

It prints out the same result?? no? And then why did you skip the "Total" word.. how would awk "add" a word ?? I thought it would just skip it?

But you're in inspiration, and I'm giving u 8 points, and the nice lady above 2 points.

Vassilios
Frequent Advisor

Re: Awk problem - addition?

I'm just curious about one thing. In a directory where there are long listings (i.e. 100's of files), whether I use my way, or your way, the figures change.

It doesn't seem to be stable when adding...

Why is that??

Try it out..

Here's my way:

ls -l | awk '{SUM += $5};END{printf "%d\n", SUM}'

James R. Ferguson
Acclaimed Contributor

Re: Awk problem - addition?

Hi (again):

> But I'm just curious, what's wrong with this?

> ls -l | awk '{sum += $5};END{printf "%d\n", sum"

> It prints out the same result?? no? And then why did you skip the "Total" word.. how would awk "add" a word ?? I thought it would just skip it?

The answer is that there is no fifth (5th) column for the "total" heading line, so 'awk' adds zero as it were. I chose to explicitly skip the "total" line simply to be pure and rigorous.

> I'm just curious about one thing. In a directory where there are long listings (i.e. 100's of files), whether I use my way, or your way, the figures change.

I suspect that you have entries special files (character and block devices). Sockets have a size of zero so those wouldn't matter. My second suggestion confined the totals to only *regular* files --- not directories nor special files nor symbolic links.

If we want to be pedantic we should probably account for hard-linked files too :-))

Regards!

...JRF...

Vassilios
Frequent Advisor

Re: Awk problem - addition?

thanks everyone. You were all brilliant. To whom do I owe the points?