move folder to another partition in linux

How to Move Directory to Another Partition in Linux

In every Linux system, there are several folders & directories that grow over time and take up a lot of disk space. For example, /home folder stores all user non-root user accounts and their files. Similarly, /var directory contains log files that grows rapidly over time. When these folders grow unchecked, this can slow down your system performance as well as boot speed. In such cases, it is advisable to move these big folders to a new partition. In this article, we will learn how to move directory to another partition in Linux.


How to Move Directory to Another Partition in Linux

Here are the steps to move directory to another partition. We have assumed that you already have another partition ready for this purpose. If not, then you will need to create a new partition.

First, list all the partitions available on your disk.

# df -l

Next, for our example, we will create a folder /srv/home to mount partition /dev/sb1, where we will copy/move our /home folder. We will create a new folder /srv/home and mount /dev/sb1 at this point.

# mkdir -p /srv/home
# mount /dev/sdb1 /srv/home 

Next, move contents of /home to /srv/home. In this case, the contents will be stored at /dev/sb1 using rsync or cp command.

# rsync -av /home/* /srv/home/
OR
# cp -aR /home/* /srv/home/

Next, we will look for any differences between /home and /srv/home. Typically, there won’t be any difference unless there is a network or hardware failure.

# diff -r /home /srv/home

Next, we delete contents of /home folder.

# rm -rf /home/*

Next, we will unmount /srv/home folder.

# umount /srv/home

Please note, at this point, your current partition will have an empty /home directory. So we mount /dev/sb1 to /home folder location.

# mount /dev/sdb1 /home
# ls -l /home

Now, when Linux refers to /home and its subfolders and files, they will be accessed from /dev/sb1 partition. But these changes will last only till next reboot. If you want to make these changes permanent, you need to first get partition UUID with the following command. Replace /dev/sdb1 with your partition which contains /home contents.

# blkid /dev/sdb1

/dev/sdb1: UUID="e087e709-20f9-42a4-a4dc-d74544c490a6" TYPE="ext4" PARTLABEL="primary" PARTUUID="52d77e5c-0b20-4a68-ada4-881851b2ca99"

In the above output, note the UUID variable, which contains partition ID.

UUID="e087e709-20f9-42a4-a4dc-d74544c490a6"

Open /etc/fstab file in a text editor.

$ vi /etc/fstab

Add the following lines.

UUID=e087e709-20f9-42a4-a4dc-d74544c490a6   /home   ext4   defaults   0   2

Save and close the file. Here is what the different fields above mean.

  • UUID – specifies block device, you can alternatively use the device file /dev/sdb1.
  • /home – mount point
  • etx4 – filesystem type of the device/partition.
  • defaults – mount options, (here this value means rw, suid, dev, exec, auto, nouser, and async).
  • 0 – used by dump tool, 0 meaning don’t dump if filesystem is not present.
  • 2 – used by fsck tool for discovering filesystem check order, this value means check this device after root filesystem.

Now if you reboot the system, you will note that the /home directory has been successfully mounted from another partition, on boot. You can run the following command to verify this.

# df -hl

That’s it. In this article, we have learnt how to move directory to another partition in Linux.

Also read:

How to Search PDF File in Linux
How to Schedule Task in Python
How to Import Python Modules by String Name
Call Python Function by String Name
How to Save Git Username & Password

Leave a Reply

Your email address will not be published. Required fields are marked *