在Linux中获取HDD的可用空间 [英] Get free space of HDD in linux

查看:57
本文介绍了在Linux中获取HDD的可用空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bash脚本中,我需要获取磁盘的总大小和整个磁盘的当前使用大小.

Within a bash script i need to get the total disk size and the currently used size of the complete disk.

我知道我可以使用以下命令获得总磁盘大小,而无需成为root用户:

I know i can get the total disk size without needed to be root with this command:

cat /sys/block/sda/size

此命令将输出设备SDA上的块数.将其乘以512,您将获得此设备上的字节数.总磁盘大小就足够了.

This command will output the count of blocks on device SDA. Multiply it with 512 and you'll get the amount of bytes on this device. This is sufficient with the total disk size.

现在输入当前使用的空间.我想获得这个值而无需root.我可以假设设备名称为SDA.现在有以下命令: df 我以为我可以使用此命令,但似乎此命令仅输出当前已安装分区的数据.

Now for the currently used space. I want to get this value without being root. I can assume the device name is SDA. Now there is this command: df I thought i could use this command but it seems this command only outputs data of the currently mounted partitions.

是否有一种方法可以获取磁盘SDA上的全部空间而无需成为root用户,并且并非所有分区都需要挂载?

Is there a way to get a total of space used on disk SDA without needing to be root and not all partitions needs to be mounted?

让我们假设以下示例:

/dev/sda1 80GB Linux partition 20GB Used
/dev/sda2 80GB Linux Home partition 20GB Used
/dev/sda3 100GB Windows Parition. 30GB Used

让我们假设我的SDA磁盘已如上所述进行分区.但是当我在Linux上时,我的Windows分区(sda3)尚未安装.

Let's assume my SDA disk is partitioned like above. But while i'm on Linux my Windows partition (sda3) is not mounted.

df的输出将给我总计40 GB的已用空间,因此无需考虑sda3.

The output of df will give me a grand total of 40 GB Used so it doesn't take sda3 in account.

再问一个问题:有没有一种方法无需root ,可以为我总共获得70 GB的已用空间?

Again the question: Is there a way without root to get me a grand total of 70 GB used space?

推荐答案

我认为 stat 命令应该有所帮助.您可以从/proc/partitions 获取分区.

I think stat command should help. You can get the get the partitions from /proc/partitions.

示例 stat 命令输出:

$ stat -f /dev/sda1
  File: "/dev/sda1"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 237009     Free: 236970     Available: 236970
Inodes: Total: 237009     Free: 236386

这篇关于在Linux中获取HDD的可用空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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