Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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.

share|improve this answer
Is that 2>/dev/null (with the >)? – ignis Apr 27 '13 at 17:55
Er yeah. Fixed. Original command would probably have you waiting a while for it to complete, heh. – Bratchley Apr 28 '13 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?

share|improve this answer
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 – Peace Blaster Apr 22 '13 at 15:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.