Sunday 11 July 2004

Read, write and move files to and from an MS-DOS formatted floppy in Linux

I have been asked a number of times as to whether Linux can read files from an MS-DOS formatted floppy. And the answer is yes it can and the method to do it is to mount the MS-DOS formatted floppy to a mount point and then read the files from there. Usually in Linux your first floppy drive will be allocated the device name /dev/fd0. So you can mount the floppy to the mount point /mnt as follows :
# mount -t msdos /dev/fd0 /mnt
And then you can access the files from the /mnt directory. Once you have mounted a device, you have to remember to unmount it before you detach the device. So in the case of the floppy, before you remove it from the floppy drive, you have to run the command:
# umount /mnt
Easier method :
But there is an easier way of reading and writing to a floppy which is formatted with an MS-DOS file system such as FAT16. That is by use of the commands in the mtools package which is usually installed by default in most major Linux distributions. A cursory look at the mtools package shows that there are around 26 command line tools in it. But the beauty of mtools is that if you have ever used commands in MS-DOS you will be perfectly at home using the commands in mtools package too.

First to get an idea of the different commands bundled with mtools, fire up a terminal and execute the command 'mtools' as follows:
$ mtools
Supported commands:
mattrib, mbadblocks, mcat, mcd, mclasserase, mcopy, mdel, mdeltree
mdir, mdoctorfat, mdu, mformat, minfo, mlabel, mmd, mmount
mpartition, mrd, mread, mmove, mren, mshowfat, mtoolstest, mtype
mwrite, mzip
From the above output, you will recognize a number of msdos commands such as copy, attrib, deltree, dir, format and so on but with the exception that they have the letter 'm' prepended to it. And these commands accomplish the same tasks as those of the MSDOS counterpart.

For instance to list the contents of a MSDOS formatted floppy, insert the floppy in the floppy drive and run the following command :
$ mdir a:
And it will display all the files and directories in the floppy. Similarly to copy a file from the Linux partition to the floppy, run the mcopy command as follows :
$ mcopy /home/ravi/test.txt a:/.
... and the file 'test.txt' gets copied to the floppy.

To format the floppy with an MSDOS file system, use the 'mformat' command and so on.

One thing worth noting is that to use mtools, you do not have to mount the floppy which is a plus point of these set of tools. You can learn more about the respective commands by viewing the man page of each of them.