为什么 du 和 Perl's -s 给出不同的文件大小值?

发布于 2024-09-24 16:43:39 字数 2200 浏览 0 评论 0原文

根据评论更新:

我有outlog.txt文件,其中包含多个文件名,例如:2345_535_Dell&HP_3PAR_DEAL.txt,同样有很多文件名但不是文件所在的实际文件夹,因此在代码中将文件名附加到文件夹路径以获取实际文件位置。现在,我想获取 outlog.txt 中存在的所有文件的磁盘使用情况以及 outlog.txt 中存在的所有文件的总磁盘使用情况。

我尝试了两种方法 perl -s 和我的 ($size) = split(' ', du `"$folderpath/$_"`) 但两种方法都给了我不同的值,而且当我我正在使用 du 而不是获得一些数值,但它没有给我单位,有没有一种方法可以让我在不使用 -h 选项的情况下获得人类可读,因为它不起作用在我的系统上?

背景信息

我的目标是获取文件的大小,目前我正在使用 perl -s 获取文件大小。我也尝试过 du ,但同一文件的大小得到了不同的值。我无法理解这是如何工作的。

问:为什么duperl -s给出不同的大小值?他们内部如何运作?这两个不同的值哪一个更准确?另外,我不确定为什么 du -h filename 给我一个非法表达式错误:

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

代码:

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

礼貌:RickF

更新:

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

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

更新代码:根据大脑的方法,但 du 值仅打印为零而不是实际值,有什么建议吗?

更新: 如果我使用 my ($size) = split(' ', du "$folderpath/$_");比我得到 du 值但它给了我一些数字,如何在不使用 -h 选项的情况下将其变为人类可读的?

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

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

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

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

发布评论

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

评论(4

梦魇绽荼蘼 2024-10-01 16:43:39

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

您还会发现稀疏文件的大小有所不同。稀疏文件占用的空间比他们声称的要少。

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.

岁月流歌 2024-10-01 16:43:39

默认情况下,du 显示文件使用的块数(在大多数系统上每个块为 512 字节),而 perl 的 -s 显示字节数。

至于为什么您的 du 副本没有 -h 选项,您没有告诉我们您正在使用什么操作系统;它似乎包含该程序的一个非常过时的版本。

更新:要获取 perl 中的磁盘使用情况,您可以使用 Filesys ::DiskUsage 模块。

By default, du displays the number of blocks used by a file (where each block is 512 bytes on most systems), while perl's -s displays bytes.

As to why your copy of du doesn't have a -h option, you don't tell us what operating system you're using; it appears to include a horribly outdated version of the program.

UPDATE: to get disk usage in perl, you can use the Filesys::DiskUsage module.

野生奥特曼 2024-10-01 16:43:39

du 代表“已使用的磁盘”,报告磁盘上文件的物理大小。如果文件稀疏,则这可能比其逻辑大小小得多,这就是 -s 报告的内容。两者都是“准确的”,它们只是测量不同的东西。

该错误消息表明您计算机上安装的 du 版本不支持 -h 选项。

du stands for "Disk Used", and reports the physical size of the file on disk. If the file is sparse, this may be much smaller than its logical size, which is what -s reports. Both are "accurate", they're just measuring different things.

The error message indicates that the version of du installed on your machine doesn't understand the -h option.

时光与爱终年不遇 2024-10-01 16:43:39

如果您希望 du 给出与 Perl 的 -s 相同的结果,请尝试 du -b。如果您的 du 支持它,这会给出“表观大小”,这与磁盘使用情况不同,正如其他人所说。

但要做到这一点,您必须更新您的 du

OP更新代码的更新:确保该文件存在于您当前的工作目录中。您可能必须在目录前面加上前缀以确保 Perl 能够找到该文件。

如果您不再在任何地方使用 $_,它也可能会澄清一些事情:

while( my $line = <$IN> ) {
  chomp $line;
  my( $block_size, $blocks ) = ( stat( $line ) )[11,12];
  ...
}

这样您就可以避免对 $_ 的无意更改。

If you want du to give the same results as Perl's -s, try du -b. If your du supports it, this gives the "apparent size", which is different from disk usage, as others have stated.

But to do this you'll have to update your du.

Update for OP's updated code: Make sure that the file exists within your current working directory. You may have to prepend the directory to make sure that Perl is finding the file.

It may also clarify things if you get away from using $_ everywhere:

while( my $line = <$IN> ) {
  chomp $line;
  my( $block_size, $blocks ) = ( stat( $line ) )[11,12];
  ...
}

This way you're safe from unintentional changes to $_.

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