Jun 192011
 

Hi All,

Recently, I had to copy over some files from one computer to another.
Using the network was too slow, but using a USB stick wasn’t quite what I needed as the files I was moving over had permissions that needed to be kept.
However the USB stick was formatted in FAT32 and I did not want to reformat into a Linux friendly format as I used the USB stick on other operating systems.

One of the solutions that can be used is to create a tarball ( .tar file ) of all the files. This is the easiest method of doing it, but is a bit inconvenient if you have to modify the files inside the tarball as you would have to untar it first, modify the files, and then retarball it all.

The solution that I use is to create a file on Linux that we can use as a container for a Linux file system. Then we can place this file onto the USB stick and mount it as though it were a normal file system.

Step 1 – Creating the container

First thing we need to do is create the file that is going to contain the file system.
Running this at the command line will create a file full of nothingness.
This can be run from anywhere, I’m running it from my home directory but you can run it on the usb stick itself –


dd if=/dev/zero of=usblinux bs=1M count=512

What that command does is read 1M of zeroes and writes it to the file ‘usblinux’ 512 times, in effect creating a file full of zeroes.

Step 2 – Creating the file system

Next up we need to create the file system that we want to use.
For me, I’m using an ext2 file system but you can use what you want.
The following command will need to be run as root, what it does is create a file system inside the file ‘usblinux’


mke2fs -t ext2 usblinux

Step 3 – Mounting the file

Now that we have a special file with a file system inside, we can mount it and use it as a file system as if it were a hard drive.
First step is to make the mount point first, then after we make the mount point we can then mount the file.


mkdir mount
mount -o loop usblinux mntdir

Step 4 – Moving the file to the USB

With it mounted, you won’t be able to move the file, so you will need to unmount the file first before you move it –


umount mntdir

Once unmounted, you can move your newly created filesystem to the USB drive, and mount it on any machine like a normal file system and use it to keep Linux files as they should be kept.

Share