1

Is it possible to check if a file path points to a ssd or hdd?

For example a script could do more operations in parallel when running on a SSD while on a HDD it would hurt performance.

2
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
2
  • I have an external drive called "Mobile TM", awk 'NR==2{print $NF}') doesn't work there. Using diskutil info "$(df /path/to/file | sed -n '2s|^.*% */|/|p')" would solve that, but the Solid State: Info not available output I get for the drive is more difficult to handle :-)
    – nohillside
    Mar 12 '21 at 9:19
  • 1
    @nohillside Thanks, edited answer with that
    – grg
    Mar 12 '21 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .