13

You can do 'ls -l' to get a detailed directory listing like this:

-rw-rw-rw-  1 alice themonkeys 1159995999 2008-08-20 07:01 foo.log
-rw-rw-rw-  1 bob   bob         244251992 2008-08-20 05:30 bar.txt

But notice how you have to slide your finger along the screen to figure out the order of magnitude of those file sizes.

What's a good way to add commas to the file sizes in the directory listing, like this:

-rw-rw-rw-  1 alice themonkeys 1,159,995,999 2008-08-20 07:01 foo.log
-rw-rw-rw-  1 bob   bob          244,251,992 2008-08-20 05:30 bar.txt
dreeves
  • 26,430
  • 45
  • 154
  • 229
  • see http://unix.stackexchange.com/questions/44898/output-ls-l-size-field-with-digits-grouped-by-thousands – golimar May 30 '13 at 15:09

10 Answers10

17

I just discovered that it's built-in to GNU Core Utils and works for ls and du!

ls -l --block-size="'1"
du --block-size="'1"

It works on Ubuntu but sadly doesn't on OSX. More on variants of block size here

seq3
  • 328
  • 2
  • 5
  • Absolutely perfect; you can even add `BLOCK_SIZE="'1"` to your profile and not have to worry about passing command line flags or setting up aliases. – Ben Blank Oct 29 '22 at 23:43
10

If the order of magnitude is all you're interested in, ls -lh does something like this:

-rw-r----- 1 alice themonkeys 626M 2007-02-05 01:15 foo.log
-rw-rw-r-- 1 bob   bob        699M 2007-03-12 23:14 bar.txt
JB.
  • 40,344
  • 12
  • 79
  • 106
  • Nice! Thanks! I didn't know about the -h switch. For most everyday use, that's probably better than adding commas. – dreeves Jan 16 '09 at 08:24
9

I don't think 'ls' has exactly that capability. If you are looking for readability, 'ls -lh' will give you file sizes that are easier for humans to parse.

-rw-rw-rw-  1 alice themonkeys 1.2G 2008-08-20 07:01 foo.log
-rw-rw-rw-  1 bob   bob        244M 2008-08-20 05:30 bar.txt
postfuturist
  • 22,211
  • 11
  • 65
  • 85
2

Here's an improvement to commafy.pl, it allows you to use ls with or without listing the file sizes. Alias ls to commafy.pl to use it.

#!/usr/bin/perl
# Does ls and adds commas to numbers if ls -l is used.

$largest_number_of_commas = 0;
$result = `ls -C @ARGV`;

# First line adds five spaces before file size
$result =~ s/(^[-lrwxds]{10,}\s*[^\s]+\s*[^\s]+\s*[^\s]+)/$1     /gm;
$result =~ s/(.{5} )(\d{4,}) /truncatePre($1,$2).commafy($2).' '/eg;

$remove_extra_spaces = 5 - $largest_number_of_commas;
$result =~ s/(^[-lrwxds]{10,}\s*[^\s]+\s*[^\s]+\s*[^\s]+) {$remove_extra_spaces}/$1/gm;
print $result;

# adds commas to an integer as appropriate
sub commafy
{
  my($num) = @_;
  my $len = length($num);
  if ($len <= 3) { return $num; }
  return commafy(substr($num, 0, $len - 3)) . ',' . substr($num, -3);
}

# removes as many chars from the end of str as there are commas to be added
# to num
sub truncatePre
{
  my($str, $num) = @_;
  $numCommas = int((length($num)-1) / 3);
  if ($numCommas > $largest_number_of_commas) {$largest_number_of_commas = $numCommas}
  return substr($str, 0, length($str) - $numCommas);
}
Eric Klien
  • 539
  • 5
  • 11
2

This common sed script should work:

ls -l | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'

However, I agree with the earlier comment suggesting ls -lh is probably the better general solution for the desired effect.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
2

This is on OS X, so you might have to tweak it a bit for your Unix flavor. I created such a function for this purpose in my ~/.bashrc dot file. The trick is using ' in the awk printf format string for the file size. A caveat: the awk mangles the "total" first line somewhat, and loses terminal coloration as well. Otherwise, one of its merits is that it tries to keep columns aligned as much as possible. To me this instantly gives a visual estimation of how big a file is. The -h switch solution is okay, but your brain needs to convert those Ks, Bs, Gs. The biggest advantage to the solution below is that you can pipe it to sort and sort would understand it. As in "lc | sort -k5,5nr" for example.

lc() {
    /bin/ls -l -GPT | /usr/bin/awk "{
        printf \"%-11s \", \$1;
        printf \"%3s \",   \$2;
        printf \"%-6s \",  \$3;
        printf \"%-6s \",  \$4;
        printf \"%'12d \", \$5;
        printf \"%3s \",   \$6;
        printf \"%2s \",   \$7;
        for (i=8; i<=NF; i++) {
            printf \"%s \", \$i
        };
        printf \"\n\";
    }"
}
1

Here's a perl script that will filter the output of 'ls -l' to add the commas. If you call the script commafy.pl then you can alias 'ls' to 'ls -l | commafy.pl'.

#!/usr/bin/perl -p
# pipe the output of ls -l through this to add commas to numbers.

s/(.{5} )(\d{4,}) /truncatePre($1,$2).commafy($2).' '/e;


# adds commas to an integer as appropriate  
sub commafy
{
  my($num) = @_;
  my $len = length($num);
  if ($len <= 3) { return $num; }
  return commafy(substr($num, 0, $len - 3)) . ',' . substr($num, -3);
}

# removes as many chars from the end of str as there are commas to be added
#   to num
sub truncatePre
{
  my($str, $num) = @_;

  $numCommas = int((length($num)-1) / 3);

  return substr($str, 0, length($str) - $numCommas);
}
dreeves
  • 26,430
  • 45
  • 154
  • 229
1

Actually, I was looking for a test for a young trainee and this seemed ideal. Here's what he came up with:

for i in $(ls -1)
do
    sz=$(expr $(ls -ld $i | awk '{print $5}' | wc -c) - 1)
    printf "%10d %s\n" $sz $i
done

It gives the order of magnitude for the size in a horribly inefficient way. I'll make this community wiki since we're both interested how you rate his code, but I don't want my rep suffering.

Feel free to leave comments (be gentle, he's a newbie, though you wouldn't guess it by his shell scripting :-).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

I wrote this several years ago, works on stdin;

Read stdin & insert commas in numbers for readability emit to stdout. example;

$ ls -l testdatafile.1000M 
-rw-rw-r--+ 1 mkm  wheel  1048576000 Apr 24 12:45 testdatafile.1000M

$ ls -l testdatafile.1000M  | commas
-rw-rw-r--+ 1 mkm  wheel  1,048,576,000 Apr 24 12:45 testdatafile.1000M

https://github.com/mikemakuch/commas

Mike Makuch
  • 1,788
  • 12
  • 15
0
export LS_BLOCK_SIZE="'1"

will do this for recent versions of GNU ls.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31