在获取带有'ls -l'的目录列表时,如何用逗号显示文件大小?

时间:2009-01-16 08:10:15

标签: unix command-line filesystems sysadmin

您可以执行'ls -l'获取详细的目录列表,如下所示:

-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

但请注意你必须沿屏幕滑动手指以确定这些文件大小的数量级。

在目录列表中为文件大小添加逗号有什么好方法,如下所示:

-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

8 个答案:

答案 0 :(得分:10)

如果您感兴趣的是数量级,ls -lh会执行以下操作:

-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

答案 1 :(得分:8)

我不认为'ls'具有这种能力。如果您正在寻找可读性,'ls -lh'将为您提供更易于人类解析的文件大小。

-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

答案 2 :(得分:2)

这个常见的sed脚本应该可以工作:

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

但是,我同意早先的评论,表明ls -lh可能是更好的通用解决方案,以达到理想的效果。

答案 3 :(得分:2)

我刚刚发现它内置于GNU Core Utils并适用于ls和du!

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

它可以在Ubuntu上运行,但遗憾的是它不适用于OSX。有关块大小here

的变体的更多信息

答案 4 :(得分:1)

这是一个perl脚本,它会过滤“ls -l”的输出以添加逗号。 如果您调用脚本commafy.pl,那么您可以将'ls'别名为'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);
}

答案 5 :(得分:1)

实际上,我正在为一名年轻的实习生寻找测试,这看起来很理想。以下是他提出的建议:

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

它以极其低效的方式给出了大小的数量级。我会制作这个社区维基,因为我们都对你的代码评价感兴趣,但我不希望我的代表遭受痛苦。

随意发表评论(温柔,他是新手,虽然你不会通过他的shell脚本来猜测: - )。

答案 6 :(得分:1)

这是对commafy.pl的改进,它允许您使用或不使用列出文件大小的ls。别名lscommafy.pl使用它。

#!/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);
}

答案 7 :(得分:1)

这是在OS X上,所以你可能需要为你的Unix风格稍微调整一下。我在〜/ .bashrc点文件中为此创建了这样一个函数。诀窍是使用&#39;在awk printf格式字符串中的文件大小。一个警告:awk破坏了&#34;总计&#34;第一线有点,也失去终端着色。否则,它的一个优点是它试图尽可能地保持列对齐。对我来说,这可以立即直观地估计文件的大小。 -h开关解决方案没问题,但你的大脑需要转换那些Ks,Bs,Gs。下面解决方案的最大优点是你可以管它进行排序和排序会理解它。如同&#34; lc | sort -k5,5nr&#34;例如。

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\";
    }"
}