当使用“ls -l”获取目录列表时,如何用逗号显示文件大小?

发布于 2024-07-12 03:03:42 字数 454 浏览 9 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

太傻旳人生 2024-07-19 03:03:42

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

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

它可以在 Ubuntu 上运行,但遗憾的是不能在 OSX 上运行。 有关块大小变体的更多信息此处

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

落墨 2024-07-19 03:03:42

如果您只关心数量级,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

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
阪姬 2024-07-19 03:03:42

我不认为“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

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
羁〃客ぐ 2024-07-19 03:03:42

这是对 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);
}

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);
}
旧梦荧光笔 2024-07-19 03:03:42

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

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

但是,我同意之前的评论,建议 ls -lh 可能是达到所需效果的更好的通用解决方案。

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.

烟凡古楼 2024-07-19 03:03:42

这是在 OS X 上,因此您可能需要根据您的 Unix 风格对其进行一些调整。 我在 ~/.bashrc 点文件中为此目的创建了这样一个函数。 诀窍是在 awk printf 格式字符串中使用 ' 来表示文件大小。 需要注意的是:awk 在某种程度上破坏了“总”第一行,并且也丢失了终端颜色。 否则,它的优点之一是它尝试使列尽可能保持对齐。 对我来说,这可以立即直观地估计文件有多大。 -h 开关解决方案没问题,但你的大脑需要转换那些 K、B、G。 下面的解决方案的最大优点是您可以通过管道将其排序并且排序会理解它。 例如“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\";
    }"
}

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\";
    }"
}
_蜘蛛 2024-07-19 03:03:42

这是一个 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);
}

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);
}
过去的过去 2024-07-19 03:03:42

事实上,我正在为一名年轻的实习生寻找测试,这似乎很理想。 这就是他的想法:

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 脚本猜到它:-)。

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 :-).

假装不在乎 2024-07-19 03:03:42

这是我几年前写的,在 stdin 上工作;

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

$ 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

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

自找没趣 2024-07-19 03:03:42
export LS_BLOCK_SIZE="'1"

将为最新版本的 GNU ls 执行此操作。

export LS_BLOCK_SIZE="'1"

will do this for recent versions of GNU ls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文