Search code examples
linuxubuntuserial-number

On Linux - How do I get an HDD serial number of a specific partition?


I need to create a tool that once launched at some directory should print the HDD serial number that the directory exists in.

Is there any way on Linux (e.g. Ubuntu 16.10) to get the HDD SN by path /dev/... ?

I've tried various tools (lsblk, hdparam, ...) but haven't succeeded.


Solution

  • You could use df . to get a device name on which your folder is located. Then using a name of your device (will be something like /dev/sda2) you could run this command:

    udevadm info --query=all --name=NAME_OF_DEVICE_FROM_FIRST_COMMAND | grep ID_SERIAL
    

    The command in 1 line:

    df . | tail -1 | awk {'print $1'} | (read name; udevadm info --query=all --name=$name) | grep ID_SERIAL