Shell Script Question

Not sure where to put this, since there isn’t a category for script questions. Maybe there should be one?

I’ve been looking for a simple command in Terminal to sum all the file sizes as reported by the ls command. I have yet to find one so I came up with this rather long winded thing.

First some data to work with, very short example (ls -l):

-rw-r--r-- 1 qruqs qruqs   119783380 18 aug 14.42  20240818-002.066
-rw-r--r-- 1 qruqs qruqs      532988 18 aug 14.36  20240818-002.par2
-rw-r--r-- 1 qruqs qruqs     3696180 18 aug 14.36  20240818-002.vol0003+0004.par2
drwxr-xr-x 8 qruqs qruqs       36864 18 aug 11.50 Deep

Then, to get to the thing I want, I have to do the following dance:

$ ls -l | grep "^-" - | cut -d "s" -f 3 | rev | cut -d " " -f 5 | rev | paste -s -d"+" | bc
30057817328

Which is the same number as reported back to me if I go to that directory and select all 81 files, right-click the selection and choose Properties.

That’s nothing short of being ridiculous! There must be a better way.

What is it?

Thanks for any ideas or solutions.

du -hs

?

1 Like

du -hs :nerd_face:

What @mithrial said was a typo, I’m sure.

du stands for disk usage
df stands for disk free
(that’s how I remember it)

man df
man du

2 Likes

I just remember it as doosh! (du -sh)

du -sh file1 file2 dir1 dir2 etc
1 Like

Then add --total and pipe the output into tail -1.

1 Like

Or if you want a list of the individual file sizes too

du -shc ./* 
1 Like

Okay, experimenting a bit I ended up with this:

$ du 2024* ba* -b --total | tail -1
30057817328     total

Much better. 8)

Thanks to all who responded. 8)

See, it isn’t too late to teach an old dog new tricks. :wink:

This will go into my tricks directory for future use.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.