14

I'm trying to find out the total size of my hard drive using df -h, but it prints the size of each partition and requires sudo.

Is there any way to get total size of my hard drive using the command line, without requiring root permission?

  • 5
    Note: Using df is imprecise if the disk has unpartitioned space, not all partitions are mounted (e.g., some Windows partitions on the same drive) or some partitions are not mounted directly but indirectly through RAID, LVM or LUKS (encryption). It will also miss the swap partition(s). So I recommend not to use df for finding out the HDD size. – Philipp Wendler Oct 28 '11 at 10:43
12

A hacky way is to bypass the need for sudo by reading out the system log with:

dmesg | grep blocks

Please note that this might not be ideal, so your mileage may vary...

4
cat /sys/block/sda/size

But the size is in block-based unit
I found this answer here.
Other solutions here.

Or you can try udisks

udisks --show-info /dev/sda | egrep "^[[:space:]]*size" | awk '{print $2}'
3

If you want to use that information in a script for example, using

sudo blockdev --getsz /dev/sda

might be easier than fdisk or hdparm as it only gives you the relevant information (just multiply by 512). However, it will also require sudo.

You could of course change the configuration of sudo such that it allows to execute this specific command without asking for the password (I guess blockdev --getsz is pretty safe even when executed by a normal non-privileged user).

This would be done by adding the following line to /etc/sudoers:

ALL ALL= NOPASSWD:/sbin/blockdev --getsz /dev/*

When you edit this file, be sure to use the command sudo visudo and not your usual editor. Otherwise it is very easy to make a syntax error, which would result in not being able to use sudo anymore (you would have to reboot into rescue mode to fix this).

  • 1
    From the man page: --getsize64 Print device size in bytes.. Also here. – Sparhawk Aug 2 '14 at 5:08
3

If you want the size in bytes and only the value e.g. for scripting:

lsblk -b --output SIZE -n -d /dev/sdX
12220202

-b: Output in bytes.
-n: No headings. We only want the pure number value.
--output SIZE: Only print the size-column.
-d /dev/sdXn: The device which size we want to know. X is e.g. d, n is e.g, 1 for first partition of disk d.

Advantages:

  • root privileges are not needed
  • grep is not needed
  • lsblk is available on most linux systems
1

You either have a bad install or an absurdly paranoid sysadmin. I've never heard of df requiring root privileges before. And I've worked with quite a few different flavors of unix/unix-like operating systems.

Anyhow, this will give you the total size of all disk partitions (first df column is /dev/hd... or /dev/sd...) in blocks (kB):

df | grep '^/dev/[hs]d' | awk '{s+=$2} END {print s}'

or in GB:

df | grep '^/dev/[hs]d' | awk '{s+=$2} END {print s/1048576}'
0

I'm not at my Linux box right now, but you could try:

hdparm -I /dev/sda

or

fdisk -l

which will also probably require a sudo.

I haven't seen anywhere that df requires root. That's odd.

  • Maybe "df" is not in PATH. HVNSweeting, try "sudo which df" and then try that as user. – Milan Babuškov Oct 27 '11 at 17:33
  • hi, this is what i get: hvnsweeting@hvnbox:~$ blockdev --getsz /dev/sda /dev/sda: Permission denied hvnsweeting@hvnbox:~$ hdparm -I /dev/sda /dev/sda: Permission denied hvnsweeting@hvnbox:~$ fdisk -l hvnsweeting@hvnbox:~$ – HVNSweeting Oct 28 '11 at 1:25
  • 1
    Everything which accesses the devices directly (i.e., /dev/sda) will need sudo. – Philipp Wendler Oct 28 '11 at 5:22

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.