为什么du和Perl的-s给文件大小不同的值? [英] Why do du and Perl's -s give different values for the file size?

查看:143
本文介绍了为什么du和Perl的-s给文件大小不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新根据评论:



我有 outlog.txt 包含多个文件名,例如: 2345_535_Dell& HP_3PAR_DEAL.txt ,类似地有许多文件名,但不是文件所在的实际文件夹,因此在代码中将文件名追加到folderpath获取实际文件位置。现在,我想获得 outlog.txt 中存在的所有文件的磁盘使用情况,以及 outlog.txt



我尝试了两种方法 perl -s split('',du`$ folderpath / $ _`),但是这两种方法都给我不同的值,当我使用 du 但它不给我单位,有没有一种方式,我可以得到人类可读,不使用 -h 选项,因为它不工作在我的系统?



背景信息



我的目标是获取文件的大小, code> perl -s 获取文件大小。我也试过 du ,并得到不同的值,同一个文件的大小。



问:为什么 du perl -s 为大小提供不同的值?他们如何在内部工作?两个不同的值中哪一个是更准确的值?此外,我不知道为什么 du -h filename 给我一个非法的表达式错误:

  bash-2.03 $ du -h test.txt 
/ usr / bin / du:illegal选项 - h
用法:du [-a] [ - d] [ - k ] [ - r] [ - o | -s] [ - L] [file ...]

代码:

  my $ folderpath ='the_path'; 
打开我的$ IN,'<','path / to / infile';
my $ total;
while(< $ IN>){
chomp;
my $ size = -s$ folderpath / $ _;
print$ _ => $ size\\\
;
$ total + = $ size;
}
printTotal => $ total\\\
;

礼貌: RickF



更新:



问:
如何获取每个文件存在的磁盘使用值而不是文件大小,这意味着如何获取每个文件的du值,而不是perl -s 文件的值?



操作系统信息
uname:SunOS
uname -v:Generic_117350-39



更新代码:根据大脑的方法,但是du值只打印为零,

更新:
如果我使用我的($ size)= split('', du$ folderpath / $ _);比我得到的价值,但它给我一些数字,我怎么可以让它变成人类可读,而不使用 -h 选项?

 #!/ usr / bin / perl 
use strict;
使用警告;

my $ folderpath ='/ Project / upload';
打开我的$ IN,'<','outlog.txt';
my $ total;
while(< $ IN>){
chomp;
#my($ block_size,$ blocks)=(stat($ _))[11,12];
#my $ du_size = $ block_size * $ blocks;
my($ size)= split('',`du$ folderpath / $ _);
#my $ size = -s$ folderpath / $ _;
print$ _ => $ size\\\
;
$ total + = $ size;
}
printTotal => $ total\\\
;


解决方案

du 报告实际磁盘使用情况,Perl的 -s 报告文件的大小。因此,如果文件长度为四个字节,则其大小为四个字节,但磁盘使用量为四千字节(取决于文件系统的设置方式)。



也会看到稀疏文件的大小有所不同。稀疏文件占用的空间比它们声称的少。


Updated According to comments:

I have outlog.txt file which contains multiple filenames, e.g: 2345_535_Dell&HP_3PAR_DEAL.txt, similarly there are many filename but not the actual folder where the files are located and so in code am appending filenames to folderpath to get actual file location. Now,I want to get disk usage of all the files present in outlog.txt and also total disk usage of all files present in outlog.txt.

I have tried two approaches perl -s and my ($size) = split(' ', du `"$folderpath/$_"`) but both approaches are giving me different values and also when I am using du than am getting some numeric value but it does not give me unit, is there a way I can get human readable without using -h option as it is not working on my system ?

Background Information

My goal is to get the size of a file, and currently I am using perl -s to get filesize. I have also tried du and am getting different values for the size of the same file. I am not able to understand how this works.

Q: Why do du and perl -s give different values for size? How do they internally work? Which of the two different values is the more accurate one? Also, I'm not sure why du -h filename gives me an illegal expression error:

bash-2.03$ du -h test.txt
/usr/bin/du: illegal option -- h
usage: du [-a][-d][-k][-r][-o|-s][-L] [file ...]

Code:

my $folderpath = 'the_path';
open my $IN, '<', 'path/to/infile';
my $total;
while (<$IN>) {
    chomp;
    my $size = -s "$folderpath/$_";
    print "$_ => $size\n";
    $total += $size;
}
print "Total => $total\n";

Courtesy: RickF

Update:

Q: How can I get the disk usage value instead of file size for each file present, meaning how can I get du value for each files rather than perl -s values for file ?

OS Information uname :SunOS uname -v :Generic_117350-39

Updated Code: According to brain's approach but still du value prints as zero only and not the actual value, any suggestions ?

Update: If I use my ($size) = split(' ', du "$folderpath/$_"); than I am getting du value but it gives me some number, how can I get it into human readable without using -h option ?

 #!/usr/bin/perl
 use strict;
 use warnings;

my $folderpath = '/Project/upload';
open my $IN, '<', 'outlog.txt';
my $total;
while (<$IN>) {
    chomp;
    #my( $block_size, $blocks ) = ( stat( "$_" ) )[11,12];
    #my $du_size = $block_size * $blocks;
    my ($size) = split(' ', `du "$folderpath/$_"); 
    #my $size = -s "$folderpath/$_";
    print "$_ => $size\n";
    $total += $size;
}
print "Total => $total\n";

解决方案

du reports actual disk usage, Perl's -s reports the size of the file. So, if a file is four bytes long it will have a size of four bytes, but disk usage of four kilobytes (depending on how your filesystem is setup).

You will also see a difference in the sizes of sparse files. Sparse files take up less space than they claim to have in them.

这篇关于为什么du和Perl的-s给文件大小不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆