I need to determine in a shell script if a given block device (e.g. /dev/sda) is a compact flash disk or a regular hard drive. Google did not help me out for once, and while udevadm info --query=all --name=sda gives me lots of information, theres nothing I recognize to mean "I'm definately (not) a flash drive" or "I'm definately (not) a hard drive".

Is it possible to distinguish these two types of storage devices?

I use Ubuntu 10.10.

  • I'm not sure it is - what distinction are you trying to make? – pjc50 Apr 6 '11 at 16:43
  • Do you mean a compact flash disk hook up as an IDE device, or just a compact flash using a USB card reader? – penguin359 Apr 6 '11 at 17:19
  • @pjc50 The script may run on a computer with a single CF disk, one CF and one IDE disk, or a single IDE disk, so I need to distinguish between the first and the last case since the script will have to do different things in those two cases. @penguin359 I mean a flash disk as an IDE device. – Lauritz V. Thaulow Apr 6 '11 at 18:37
  • 1
    Try hdparm -I, which shows the rotation speed (RPM, meaningless on a CF) on some drives. Many drives don't report the info though; perhaps there's another parameter you can use. – Gilles Apr 6 '11 at 23:06
  • @Gilles Bingo for hdparm -I, the very first line after the device name says either "CompactFlash ATA device" or "ATA device, with non-removable media". If you'd like to post your comment as an answer, I'll accept it. – Lauritz V. Thaulow Apr 7 '11 at 7:27

It seems likely (though there may be caveats) that this will distiguish between flash-based storage devices and traditional hard disks:

is_compact_flash ()
    hdparm -I $1 | sed -n '\_Commands/features:_,\_Security:_p' | \
        grep -q CFA && return 0 || return 1

if is_compact_flash $disk; then
    echo "$disk is a compact flash disk"
    echo "$disk is not a compact flash disk"

EDIT: Added sed command to guard against model or serial containing CFA.

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.