如何使用 Perl 找到挂载卷上的可用空间? [英] How can I find the free space available on mounted volumes using Perl?

查看:60
本文介绍了如何使用 Perl 找到挂载卷上的可用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解压缩文件.在解压之前,我想知道已安装卷上的可用空间.计划是如果没有足够的空间我不会解压它!那么如何使用 Perl 找到已安装卷上的可用空间呢?顺便说一下,我在 tar 和 untar 中使用 Perl.

每个人都在谈论 dfdh 但这些命令在挂载点上不起作用.如果我想在挂载点上找到可以写入的空闲空间怎么办?

解决方案

使用 shell 命令生成 Perl 可以使用的单个无 K 数:

进入你想要解压的目录(如果还没有)并执行:

df .|grep -v '^文件系统' |awk 'NF=6{print $4}NF==5{print $3}{}'

或者替换."与实际挂载点.

grep 去掉了标头,awk 打印出可用于拆分行和非拆分行的千字节数.

这是基于以下类型的输出,如果您的 UNIX 输出不同,您可能需要进行调整:

<前>文件系统 1K-blocks Used 可用使用% Mounted on/dev/sda4 206434224 56965356 139065176 30%/varrun 1037296 132 1037164 1%/var/runvarlock 1037296 0 1037296 0%/var/lockudev 1037296 68 1037228 1%/devdevshm 1037296 12 1037284 1%/dev/shm/dev/sda2 93327 43535 44973 50%/boot/dev/sdc1 155056484 29417456 117824612 20%/media/extra160gvfs-保险丝守护进程206434224 56965356 139065176 30%/home/pax/.gvfs

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?

解决方案

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

这篇关于如何使用 Perl 找到挂载卷上的可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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