Get free space of HDD in linux

馋奶兔 提交于 2020-08-06 12:54:59

问题


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

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

cat /sys/block/sda/size

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.

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.

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?

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. But while i'm on Linux my Windows partition (sda3) is not mounted.

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

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


回答1:


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

Sample stat command output:

$ 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



回答2:


You can use 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



来源:https://stackoverflow.com/questions/36596806/get-free-space-of-hdd-in-linux

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