How can I find the free space available on mounted volumes using Perl?

人盡茶涼 提交于 2019-12-10 04:27:07

问题


I'm trying to untar a file. Before untarring I would like to know free space available on the mounted volume. The plan is if there is not enough space I will not untar it! So how can I find the free space available on a mounted volume using Perl? By the way, I'm using Perl for tar and untar.

Everybody is saying about df and dh but these commands doesn't work on the mount points. What if I want to find the free space that I can write into on a mounted point?


回答1:


Using shell commands to generate a single K-free number which Perl can use:

Change into the directory where you want to untar (if not already there) and execute:

df . | grep -v '^Filesystem' | awk 'NF=6{print $4}NF==5{print $3}{}'

Or replace "." with the actual mount point.

The grep gets rid of the header and the awk prints out the kilobytes available for both split and no-split lines.

This is based on the following sort of output, you may have to adjust if your UNIX outputs something different:

Filesystem    1K-blocks      Used  Available  Use%  Mounted on
/dev/sda4     206434224  56965356  139065176   30%  /
varrun          1037296       132    1037164    1%  /var/run
varlock         1037296         0    1037296    0%  /var/lock
udev            1037296        68    1037228    1%  /dev
devshm          1037296        12    1037284    1%  /dev/shm
/dev/sda2         93327     43535      44973   50%  /boot
/dev/sdc1     155056484  29417456  117824612   20%  /media/extra160
gvfs-fuse-daemon
              206434224  56965356  139065176   30%  /home/pax/.gvfs



回答2:


You likely want the CPAN module Filesys::DfPortable.




回答3:


You could just use built-in linux commands to get the result:

my $vol = "/dev/volume";
my $freespace = `df $vol | grep '$vol' | awk '{print \$4}'`;
# free space in megabytes.
$freespace = sprintf("%01.2f", $freespace / 1024);



回答4:


Try Filesys::DF (posix system) or Filesys::DfPortable (Windows also). They both use native code to enumerate FS statistics.



来源:https://stackoverflow.com/questions/536160/how-can-i-find-the-free-space-available-on-mounted-volumes-using-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!