Get free hard disk space on Linux

Inside the bash script, I need to get the total disk size and the currently used full disk size.

I know that I can get the total disk size without having to root with this command:

cat /sys/block/sda/size 

This command will display the number of blocks on the SDA device. Multiply it by 512 and you will get the number of bytes on this device. This is enough for the total disk size.

Now for the used space. I want to get this value without root. I can assume that the device name is SDA. Now there is such a command: df I thought I could use this command, but it seems that this command only displays data from partitions already mounted.

Is there a way to get the total space used on the SDA without needing a root, and not all partitions need to be installed?

Suppose 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 

Suppose my SDA drive is partitioned as described above. But while I'm on Linux, my Windows partition (sda3) is not mounted.

The df output will give me a total of 40 GB, so it does not take an sda3 account.

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

+5
source share
2 answers

It seems to me that the stat command should help. You can get partitions from /proc/partitions .

Example 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 
+1
source

You can use df .

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

If you combine this with some sed or awk , you can get the desired value

-1
source

Source: https://habr.com/ru/post/1247032/


All Articles