# Need a quick way to add up file sizes in Linux

**URL:** <https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519>\
**Category:** Factual Questions\
**Created:** [June 3, 2004, 10:07pm UTC](https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519 "2004-06-03T22:07:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orbifold](https://avatars.discourse-cdn.com/v4/letter/o/779978/32.png) [@Orbifold](https://boards.straightdope.com/u/Orbifold)\
**Post date:** [June 3, 2004, 10:07pm UTC](https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519/1 "2004-06-03T22:07:08Z")

</div>

Say I have a bunch of files in a Linux directory called foo.1, foo.2, and so forth, and I want to know the total size of all the files. If they were the only files in the directory, I could use “du -ab --max-depth=1” and then ignore everything but the last line. But they’re not the only files in the directory, and the other files don’t match any fixed pattern that I could use with the “-exclude” option to du.

Currently, I’m using the following hideous bit of hackery:

```auto

ls -l foo.* | awk '{print $5}' | perl -ple '$_=$t+=$_' | tail -n 1

```

But there’s got to be a better way than that. Anyone know what it is?

(There’s probably some perfectly obvious Unix command that does this that I just don’t know about…)

---

<div class="post-metadata">

**Author:** ![SmackFu](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@SmackFu](https://boards.straightdope.com/u/SmackFu)\
**Post date:** [June 3, 2004, 10:17pm UTC](https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519/2 "2004-06-03T22:17:40Z")

</div>

Awk can do the math for you.

```auto

ls -l foo.* | awk '{print sum+=$5}' | tail -n 1

```

---

<div class="post-metadata">

**Author:** ![Orbifold](https://avatars.discourse-cdn.com/v4/letter/o/779978/32.png) [@Orbifold](https://boards.straightdope.com/u/Orbifold)\
**Post date:** [June 3, 2004, 10:20pm UTC](https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519/3 "2004-06-03T22:20:58Z")

</div>

See? I _knew_ there was some bit of Unix arcana I didn’t know that would help. Thanks!

(In case it’s not blindingly obvious, someone once showed me how awk could be used to extract a given column from text output and I haven’t used it for anything else since.)

---

<div class="post-metadata">

**Author:** ![JohnM](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@JohnM](https://boards.straightdope.com/u/JohnM)\
**Post date:** [June 3, 2004, 10:54pm UTC](https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519/4 "2004-06-03T22:54:05Z")

</div>

How about doing it with only one pipe?

```auto

ls -l foo.* | awk '{tot+=$5} END {print tot}'

```

---

<div class="post-metadata">

**Author:** ![Bill\_H](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@Bill\_H](https://boards.straightdope.com/u/Bill_H)\
**Post date:** [June 3, 2004, 11:16pm UTC](https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519/5 "2004-06-03T23:16:32Z")

</div>

gotcha all beat

cat foo.\* | wc -c

Ok, that’s not a great idea if your files have any size to them.

---

<div class="post-metadata">

**Author:** ![zoltar7](https://avatars.discourse-cdn.com/v4/letter/z/b38774/32.png) [@zoltar7](https://boards.straightdope.com/u/zoltar7)\
**Post date:** [June 4, 2004, 3:32am UTC](https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519/6 "2004-06-04T03:32:33Z")

</div>

du -c foo.\*

will give you a total at the end, but will also show you the size of all the files that match the expression. You could do

du -c foo.\* | tail -1

if you only want the total line. And of course it will say “total” after the total size, so you’d have to strip that off if you cared.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [June 4, 2004, 5:00am UTC](https://boards.straightdope.com/t/need-a-quick-way-to-add-up-file-sizes-in-linux/248519/7 "2004-06-04T05:00:47Z")

</div>

I’d do it all in Perl, myself:

```auto

perl -e 'for (glob "foo.*") {$sum += -s} print "$sum
";'

```

I think that’s clearer than most of the shell alternatives, and it can be trivially migrated to its own file for use as a standalone program.

(Yes, that’s tested code. I love Linux. :D)
