Unix 当使用';获取目录列表时,如何使用逗号显示文件大小;ls-l';?
您可以执行“Unix 当使用';获取目录列表时,如何使用逗号显示文件大小;ls-l';?,unix,command-line,filesystems,sysadmin,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 b
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
-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
但请注意,您必须在屏幕上滑动手指才能计算出这些文件大小的数量级
向目录列表中的文件大小添加逗号的好方法是什么,如下所示:
-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
-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
下面是一个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);
}
#/usr/bin/perl-p
#通过此管道将ls-l的输出添加到数字中。
s/({5})(\d{4})/truncatePre($1,$2)。commafy($2)。”/e;
#将逗号适当地添加到整数
亚社区
{
我的($num)=@;
my$len=长度($num);
如果($len如果数量级是您感兴趣的全部,ls-lh
执行以下操作:
-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
-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
我不认为“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
事实上,我正在为一名年轻的实习生寻找一个测试,这似乎很理想。以下是他的想法:
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脚本猜到:-)。这里是对commafy.pl的一个改进,它允许你在使用ls时列出或不列出文件大小。Aliasls
tocommafy.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);
}
!/usr/bin/perl
#如果使用ls-l,则不使用ls并在数字上添加逗号。
$maximust_number_of_逗号=0;
$result=`ls-C@ARGV`;
#第一行在文件大小之前添加五个空格
$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-$maximust_number_逗号;
$result=~s/(^[-lrwxds]{10,}\s*[^\s]+\s*[^\s]+\s*[^\s]+){$remove\u extra\u spaces}/$1/gm;
打印$result;
#将逗号适当地添加到整数
亚社区
{
我的($num)=@;
my$len=长度($num);
if($len$maximust\u number\u of逗号){$maximust\u number\u of逗号=$numCommas}
返回substr($str,0,length($str)-$numCommas);
}
此通用sed脚本应该可以工作:
ls -l | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
但是,我同意前面的评论,即对于预期效果而言,ls-lh
可能是更好的通用解决方案。这是在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\";
}"
}
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;
打印F\%2s\”,\$7;
因为(i=8;i我几年前写了这篇文章,在stdin上工作
读取stdin并在数字中插入逗号,以确保发送到stdout的可读性。
榜样
$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车轮1048576000 Apr 24 12:45 testdatafile.1000M
我刚刚发现它内置于GNU核心UTIL中,适用于ls和du
ls -l --block-size="'1"
du --block-size="'1"
它在Ubuntu上工作,但不幸的是在OSX上没有。更多关于块大小的变体的信息Nice!谢谢!我不知道-h开关。对于大多数日常使用,这可能比添加逗号要好。参见