Detect SSD/HDD from Bash Script

diskutil info "$(df /path/to/file | sed -n '2s|^.*% */|/|p')" | awk '/Solid State/ {print $NF}'

Returns Yes if solid state, No if not, and Info not available otherwise.


  • subshell df /file/path | awk 'NR==2{print $NF}'

    df /path/to/file prints information on the mount point containing the given file.
    sed replaces in the result.
    -n disables ‘automatic’ printing, i.e. only print lines that have been specifically told to print.
    2 performs the operation on the second line.
    s| performs a substitution with the arguments between occurrences of the delimiter given.
    ^ from the start of the line.
    .*% any characters any number of times, up to the last percent sign.
    */ then any number of spaces before the slash.
    |/ should be replaced with a slash.
    |p and the line should be printed.

    This returns something like /Volumes/MyVolumeName.

  • command diskutil info /Volumes/MyVolumeName | awk '/Solid State/ {print $NF}'

    diskutil info provides information about the disk or volume given.
    awk filters on the result.
    /Solid State/ returns lines containing the string "Solid State".
    {print $NF} prints the last column.


Example: my home directory is on the internal SSD which is solid state, command returns "Yes".

$ diskutil info "$(df ~ | sed -n '2s|^.*% */|/|p')" | awk '/Solid State/ {print $NF}'
Yes