I'm trying to write a bash script to run from Linux on a CD or flashdrive that can mount Windows and change stuff, but I don't know how to go about detecting which partition/drive is Windows.

My best guess would be somehow using awk to detect the largest thing in /dev, but I have no idea how to go about writing the expression.

2 Answers 2


If you feel confident that partition IDs are correct:

# fdisk -l 2>/dev/null | awk '/NTFS$/ {print $1}'

That should print the block devices for the NTFS volumes.

  • Is that 2>/dev/null (with the >)?
    – ignis
    Apr 27, 2013 at 17:55
  • Er yeah. Fixed. Original command would probably have you waiting a while for it to complete, heh.
    – Bratchley
    Apr 28, 2013 at 21:15

You can use df -T to get all the partitions and their types. Then find the partition with type NTFS (or whatever your Windows partitions are fomatted as, e.g. Fat32?) like so:

PARTITIONS=$(df -T | grep fat32 | awk '{print $1}')

Then you can use a loop to go over the partitions and do stuff with them.

A more robust solution would be to mount the potential partitions an check to see if they have e.g. a Windows directory in the root.

Does that answer your question?

  • That looks like it'll work, though my concern was that different PCs may have their drives named differently from Linux's point of view, so I'd have to check drive and partition Apr 22, 2013 at 15:15

You must log in to answer this question.

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