Stanley Krute
Stanley Krute

Reputation: 11

printing comma-separated integer size of directory and contents

The following will print out the size in bytes of a directory and its contents:

ls -lR | grep -v '^d' | awk '{bytes += $5} END {print "Total bytes: " bytes}'

The output looks like this:

Total bytes: 1088328265

How can I most-simply modify my command so that the output has comma-separated numbers, like this:

Total bytes: 1,088,328,265

Upvotes: 1

Views: 256

Answers (2)

Ed Morton
Ed Morton

Reputation: 204468

$ awk 'BEGIN{printf "Total bytes: %\047d\n", 1088328265}'
Total bytes: 1,088,328,265

So puting aside the usual advice to not parse the output of ls and getting rid of the grep since you never need grep when you're using awk, we can make your whole command:

ls -lR | awk '!/^d/{bytes += $5} END{printf "Total bytes: %\047d\n", bytes}'

\047 is how to represent a single-quote in a single-quote-delimited awk script and then from the GNU awk manual:

A single quote or apostrophe character is a POSIX extension to ISO C. It indicates that the integer part of a floating-point value, or the entire part of an integer decimal value, should have a thousands-separator character in it. This only works in locales that support such characters. For example:

$ cat thousands.awk          Show source program
-| BEGIN { printf "%'d\n", 1234567 }
$ LC_ALL=C gawk -f thousands.awk
-| 1234567                   Results in "C" locale
$ LC_ALL=en_US.UTF-8 gawk -f thousands.awk
-| 1,234,567                 Results in US English UTF locale

For more information about locales and internationalization issues, see Locales.

Upvotes: 4

choroba
choroba

Reputation: 242168

Using Perl instead of awk:

perl -lane '$bytes += $F[4];
            END { substr $bytes, -3 * $_, 0, ","
                      for reverse 1 .. (length($bytes)-1)/3;
                  print "Total bytes: $bytes"}'
  • -l removes newlines from input and adds them to prints
  • -n reads the input line by line
  • -a splits the input on whitespace into the @F array
  • substr inserts spaces to each position; we use negative positions which count from the right, but we start from the leftmost position so the numbers don't change as we add the commas

Upvotes: 0

Related Questions