Mounting to non-empty directory, then later deleting original files

administrationfilesystemsmount

I have some unused space on my disk, and decided to use a little of it to create a separate /var partition. There might be an easier way to do this, but my idea is to

  1. copy the existing /var directory to the partition;
  2. mount the partition over the existing /var directory;
  3. using another system, either another distro on the disk or a live
    CD, to delete the original /var contents while the partition is not
    mounted.

I've done the first 2 steps, and it seems OK so far. I've made an entry in fstab to mount on boot, which I'll test before deleting. But I'm wondering if this is a valid strategy, and also if it's a good idea and/or if there is a better way. It's somewhat of a learning experience for me to play around more with mounting beyond the basics.

Edit (results):

Thanks to the suggestions below, I completed step 3 without the need to use a 2nd system. The method I used was to remount the root fs using the command mount --bind / /mnt/temp, then going to /mnt/temp/var and deleting the files and directories there.

The one suggestion made after I did it was to create a dummy file, and then check the mounted /var directory to confirm that it was not seen in that directory. But I compared timestamps to see that they were different, so I felt confident that I was OK.

Thanks for the suggestions, everyone.

Best Answer

If you're on Linux you can :

  • Mount your root filesystem on a random mountpoint. I usually use /mnt for this sort of things.

    mount --bind / /mnt
    
  • Then you can (re)move the files from /mnt/var

Not all OS's allow a filesystem mounted on a second mount point at the same time, that's why I wrote 'on Linux'. Just try the mount, it won't break anything.

Related Question