Skip to content

Instantly share code, notes, and snippets.

@wenboown
Created August 23, 2025 21:02
Show Gist options
  • Select an option

  • Save wenboown/951875fa4cd28cae36c9eaeb0851e838 to your computer and use it in GitHub Desktop.

Select an option

Save wenboown/951875fa4cd28cae36c9eaeb0851e838 to your computer and use it in GitHub Desktop.
Ubuntu: Moving Large Directories from small OS SSD to HDD

Ubuntu: Moving Large Directories from small OS SSD to HDD

A comprehensive guide for migrating home directories, Docker data, and other large folders from a small SSD to a larger HDD on Ubuntu systems.

Common Scenario

  • Small SSD (50-200GB) for OS at 90%+ capacity
  • Large HDD (1TB+) mounted but underutilized
  • Need to move user data, Docker, caches to HDD while maintaining functionality

Prerequisites

# Check current disk usage
df -h

# Identify your large HDD (usually /dev/sdb, /dev/sdc, etc.)
lsblk
sudo blkid

# Mount your HDD if not already mounted
sudo mkdir -p /mnt/storage
sudo mount /dev/sdX1 /mnt/storage  # Replace X with your drive letter

Make HDD Mount Permanent

Add your HDD to /etc/fstab for automatic mounting:

# Get UUID of your HDD
UUID=$(sudo blkid /dev/sdX1 | grep -o 'UUID="[^"]*"' | cut -d'"' -f2)

# Backup fstab
sudo cp /etc/fstab /etc/fstab.backup

# Add mount entry
echo "UUID=$UUID /mnt/storage ext4 defaults,noatime 0 2" | sudo tee -a /etc/fstab

# Test the mount
sudo umount /mnt/storage
sudo mount -a

Step 1: Home Directory Migration (Recommended)

Option A: Using usermod (Cleanest)

This updates the system user database properly:

# 1. Create new home directory on HDD
sudo mkdir -p /mnt/storage/home/$(whoami)

# 2. Copy all home data (run as root or from another user session)
sudo rsync -av /home/$(whoami)/ /mnt/storage/home/$(whoami)/

# 3. Update user's home directory in system database
sudo usermod -d /mnt/storage/home/$(whoami) $(whoami)

# 4. Verify the change
getent passwd $(whoami)

# 5. Backup original home and create symlink for compatibility
sudo mv /home/$(whoami) /home/$(whoami).backup
sudo ln -s /mnt/storage/home/$(whoami) /home/$(whoami)

# 6. Reboot or re-login to activate changes

Option B: Using Bind Mount (Most Compatible)

This approach works best with snaps, containers, and all applications:

# 1. Copy home directory data
sudo mkdir -p /mnt/storage/home/$(whoami)
sudo rsync -av /home/$(whoami)/ /mnt/storage/home/$(whoami)/

# 2. Backup original and create mount point
sudo mv /home/$(whoami) /home/$(whoami).backup
sudo mkdir /home/$(whoami)

# 3. Create bind mount
sudo mount --bind /mnt/storage/home/$(whoami) /home/$(whoami)

# 4. Make permanent in /etc/fstab
echo "/mnt/storage/home/$(whoami) /home/$(whoami) none bind 0 0" | sudo tee -a /etc/fstab

# 5. Test the mount
sudo umount /home/$(whoami)
sudo mount -a

Step 2: Docker Data Migration

Docker stores large images and containers that can consume significant space:

# 1. Stop Docker
sudo systemctl stop docker

# 2. Create Docker directory on HDD
sudo mkdir -p /mnt/storage/docker

# 3. Copy existing Docker data
sudo rsync -av /var/lib/docker/ /mnt/storage/docker/

# 4. Backup original directory
sudo mv /var/lib/docker /var/lib/docker.backup

# 5. Create bind mount
sudo mkdir /var/lib/docker
sudo mount --bind /mnt/storage/docker /var/lib/docker

# 6. Add to /etc/fstab
echo "/mnt/storage/docker /var/lib/docker none bind 0 0" | sudo tee -a /etc/fstab

# 7. Start Docker
sudo systemctl start docker

# 8. Verify Docker works
docker run --rm hello-world

Note: You can do the same for other similar folders, like /var/lib/containers, etc.

Verification Commands

# Check disk usage
df -h

# Verify bind mounts
mount | grep bind

# Check Docker functionality
docker --version
docker ps

# Verify home directory
echo $HOME
ls -la $HOME

# Check environment variables
env | grep -E "(PIP_CACHE|UV_CACHE|HF_HOME)"

Troubleshooting

If Applications Can't Find Files

  • Use bind mounts instead of symlinks
  • Ensure proper ownership: sudo chown -R $(whoami):$(whoami) /mnt/storage/home/$(whoami)

If Docker Fails to Start

# Check Docker logs
sudo journalctl -u docker.service -f

# Verify Docker directory permissions
sudo ls -la /var/lib/docker

If Snap Packages Fail

  • Snap packages work better with bind mounts than symlinks
  • Ensure the bind mount is in /etc/fstab for early mounting

Recovery Plan

If something goes wrong:

# Restore home directory
sudo umount /home/$(whoami) 2>/dev/null || true
sudo rm /home/$(whoami) 2>/dev/null || true
sudo mv /home/$(whoami).backup /home/$(whoami)

# Restore Docker
sudo systemctl stop docker
sudo umount /var/lib/docker 2>/dev/null || true
sudo rm -rf /var/lib/docker
sudo mv /var/lib/docker.backup /var/lib/docker
sudo systemctl start docker

Best Practices

  1. Always backup original directories before migration
  2. Use bind mounts for better application compatibility
  3. Test thoroughly after each migration step
  4. Update environment variables for cache directories
  5. Add to /etc/fstab to make changes permanent
  6. Monitor disk usage regularly with df -h

Notes

  • Bind mounts are transparent to applications (recommended)
  • Symlinks may cause issues with snap packages and containers
  • The usermod approach is cleanest for home directories but requires re-login
  • Always test critical applications after migration
  • Consider using rsync -av to preserve permissions and timestamps

This guide has been tested on Ubuntu 22.04 LTS and should work on most Debian-based systems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment