Mount & UnMount Windows Partitions In Linux using Shell Script [ How to ]

If you dual-boot Linux and Windows, you’re likely to mount the Windows partition every now and then. Although some of the new distributions provide the option of mounting Windows partitions at startup, you might still want to keep control to yourself. You can do this in 2 ways, Manually or by using a simple shell script.

To have good control over the mounting of a partition, you need to be aware of the device name of a partition, such as /dev/hda5 (a Windows FAT32 partition on our machine, also known as the E: drive) and a mount point for it (say /mnt/win/E). This is explained in the following.

  • To see the list of all the partitions on your hard disk, use the command “fdisk -l”.
  • Note down the device name of the your Windows drives; you need to mount these partitions to a predefined folder, called the “mount point.”
  • Create a folder called “win” under /mnt, and then create folders under “win” for the partitions you want to mount. We used the drive letters (C, D, and E) as the names for the mount points.
  • When mounted, the Windows partitions would appear under /mnt/win/C, /mnt/win/D, and /mnt/win/E.
  • To mount your Windows D drive, say, you will have to issue the command

mount /dev/hdaX/mnt/win/D where the “X” in “hdaX” stands for the partition number corresponding to the Windows D drive.

  • You will have to repeat the command for all the partitions and at every boot. You’ll soon find this a tedious task!


Simplify it:

Use just one command. Create shell scripts for mounting and un-mounting the partitions. Assuming you’re using konsole or gnome-terminal, create a file using the vi editor as follows:

#vi win-mount [Enter]

This creates a file with the name “win-mount”, but it isn’t saved yet. Press [Insert] to switch to the text editing mode of vi. Enter the following commands one below the other in the vi editor:

mount /dev/hdb1
mount /dev/hdb5
mount /dev/hdb6

Save the file by first hitting [Esc] to exit from the typing/editing mode, then key in “:” (a colon) followed
with “wq”, which means save and exit.

Create another file in the same fashion to un-mount the mounted partitions. Only the file name (use “vi win-umount”) and the set of commands will undergo a few changes. The command will change to:

umount /mnt/win/C
umount /mnt/win/D
umount /mnt/win/E

Save and exit the file. The last step is to change this text file to a shell script. To achieve this, issue the
following command:

#chmod 700 win-mount
win-umount [Enter]

Both the text files will now have shell script permissions. Whenever you want to mount the Windows
partitions, change to the directory containing the script and issue

#./win-mount [Enter]

To un-mount the partitions before shutdown or whenever you want to, change to the directory.

Filed under: Linux
Tags: , , , , ,
July 17, 2009 by: Prasanth Chandra


Leave a Reply