How to move a folder to a different partition in Linux

My web server on Amazon AWS was low on disk space and I decided to move my /var folder to another partition.

After adding another 50G from the AWS console, I checked the server console to verify

cat /proc/partitions

I could see the 50G partition listed as xvdf. The next step was to make it available

mkfs.ext4 /dev/xvdf

 

Next, I create a folder newvar under the /mnt folder and mounted the partition 

mkdir /mnt/newvar

mount /dev/xvdf /mnt/newvar

The next step was to copy files from the /var to /mnt/newvar

cd /var

find . -depth -print0 | cpio --null --sparse -pvd /mnt/newvar

Once the files were copied, I unmounted the newvar folder

umount /mnt/newvar

Now I had to disconnect the /var folder and replace it with the newvar folder

mv /var /var.old

mkdir /var

mount /dev/xvdf  /var

 

The final step was to make an entry in the fstab file to ensure that the /var folder gets mounted on /dev/xvdf

vi /etc/fstab

Inser the following line:

/dev/xvdf       /var    auto    defaults        1       2

Save the file and reboot. The /var is now 50G large.