简体   繁体   English

在linux中获得HDD的可用空间

[英]Get free space of HDD in linux

Within a bash script i need to get the total disk size and the currently used size of the complete disk. 在bash脚本中,我需要获取整个磁盘的总磁盘大小和当前使用的大小。

I know i can get the total disk size without needed to be root with this command: 我知道我可以使用此命令获取总磁盘大小而无需root用户:

cat /sys/block/sda/size

This command will output the count of blocks on device SDA. 此命令将输出设备SDA上的块数。 Multiply it with 512 and you'll get the amount of bytes on this device. 将它乘以512,您将获得此设备上的字节数。 This is sufficient with the total disk size. 这足以满足总磁盘大小。

Now for the currently used space. 现在为当前​​使用的空间。 I want to get this value without being root. 我希望得到这个价值而不是根。 I can assume the device name is SDA. 我可以假设设备名称是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. 现在有这个命令: df我以为我可以使用这个命令,但似乎这个命令只输出当前安装的分区的数据。

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? 有没有办法在磁盘SDA上使用总空间而不需要root,而不是所有分区都需要挂载?

Let's assume the following example: 我们假设以下示例:

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

Let's assume my SDA disk is partitioned like above. 我们假设我的SDA磁盘是如上所述进行分区的。 But while i'm on Linux my Windows partition (sda3) is not mounted. 但是当我在Linux上时,我的Windows分区(sda3)没有安装。

The output of df will give me a grand total of 40 GB Used so it doesn't take sda3 in account. df的输出将使我总共使用40 GB,因此它不会占用sda3。

Again the question: Is there a way without root to get me a grand total of 70 GB used space? 问题又来了:有没有根本办法让我总计为70 GB的使用空间?

I think stat command should help. 我认为stat命令应该有所帮助。 You can get the get the partitions from /proc/partitions . 您可以从/proc/partitions获取/proc/partitions

Sample stat command output: 示例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

You can use df . 你可以使用df

df -h --output='used' /home 
Used
3.2G

If you combine this with some sed or awk you can have the value you seek 如果你将它与某些sedawk结合起来,你可以拥有你所寻求的价值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM