13

您可以执行 ' 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
4

9 回答 9

15

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

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

它适用于 Ubuntu,但遗憾的是不适用于 OSX。更多关于块大小的变体在这里

于 2016-03-24T13:30:31.287 回答
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
于 2009-01-16T08:19:47.720 回答
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
于 2009-01-16T08:25:09.363 回答
2

这是对 commafy.pl 的改进,它允许您使用 ls 列出或不列出文件大小。ls使用commafy.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);
}
于 2013-10-01T00:55:11.143 回答
2

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

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

但是,我同意较早的评论,建议ls -lh可能是达到预期效果的更好的通用解决方案。

于 2014-05-14T22:38:39.603 回答
2

这是在 OS X 上,所以您可能需要根据您的 Unix 风格对其进行一些调整。为此,我在 ~/.bashrc 点文件中创建了这样一个函数。诀窍是在 awk printf 格式字符串中使用 ' 作为文件大小。一个警告:awk 在某种程度上破坏了“总”第一行,并且也失去了终端颜色。否则,它的优点之一是它试图尽可能地保持列对齐。对我来说,这立即给出了文件大小的视觉估计。-h 开关解决方案还可以,但是你的大脑需要转换那些 Ks、Bs、Gs。以下解决方案的最大优点是您可以通过管道对其进行排序,并且排序会理解它。例如在“lc | sort -k5,5nr”中。

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\";
    }"
}
于 2014-10-21T15:14:57.480 回答
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);
}
于 2009-01-16T08:19:24.973 回答
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

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

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

于 2009-01-16T08:42:03.383 回答
0

我几年前写的,在标准输入上工作;

读取标准输入并在数字中插入逗号以提高可读性发射到标准输出。例子;

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

$ ls -l testdatafile.1000M | 逗号
-rw-rw-r--+ 1 mkm 车轮  1,048,576,000 Apr 24 12:45 testdatafile.1000M

https://github.com/mikemakuch/commas

于 2015-09-10T22:06:46.680 回答