DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Move WSL File System to another Drive
Camilo Martinez
Camilo Martinez

Posted on • Updated on

Move WSL File System to another Drive

Language: [πŸ‡ͺπŸ‡Έ] EspaΓ±ol - [πŸ‡ΊπŸ‡Έ] English


If the C: drive is running out of space or do you like to have your files in a different disk than your Operating System, we can move our WSL2 file system to another drive in a few steps.

Prerequisites

If we are using a username different than root, we need to know it running the whoami command (WSL side):

who am I

Also we need to know the distro name and WSL version, running the wsl --list --verbose command on PowerShell (Windows side):

wsl list

Knowing the username: equiman, the linux distribution: Ubuntu, and confirm that we are using the WSL 2 version, we are ready to...

moving day

Move

All next steep need to be done on PowerShell (Windows side).

First, we need to close all terminals that are using WSL, and then shut it down to avoid data corruption running the wsl --shutdown command.

Next we are going to create a backup from our Ubuntu linux distribution.

mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

Don't worry if you do not see progress indicators. This can take a while depending on how much data you have. Maybe it's a good idea to take a rest.

waiting

When finish, we need to to unregister the linux distribution from C: drive. It will also release the disk space taken by WSL.

⚠️ Beware that everything that was stored on the WSL drive will be lost, and the command won't ask for a confirmation, so make sure you backup everything you need first!

wsl --unregister Ubuntu
Enter fullscreen mode Exit fullscreen mode

And then, import again on our new disk. I'll use D: disk as an example.

mkdir D:\WSL
wsl --import Ubuntu D:\WSL\ D:\backup\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

By default Ubuntu will use root as default user, to switch to our user we need to run these commands.

cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
ubuntu config --default-user equiman
Enter fullscreen mode Exit fullscreen mode

Ensure replacing equiman with yours.

Restart WSL running wsl command and it's done.


That’s All Folks!
Happy Coding πŸ––

beer

Top comments (4)

Collapse
 
rudolphh profile image
Rudy A. Hernandez • Edited on

Thanks for this.

Note: the 'wsl' command at the end wouldn't work because the default was set to another distribution (if you have any others like docker-desktop). You can specify the distribution with:

wsl -d Ubuntu
Enter fullscreen mode Exit fullscreen mode

or set the default distribution for wsl:

wsl --setdefault Ubuntu
wsl
Enter fullscreen mode Exit fullscreen mode
Collapse
 
adame11 profile image
adame11

Thank you for this guide! Ubuntu for example will no longer appear in Apps and Features (if installed via Microsoft Store ... "Ubuntu by canonical etc") but that was no matter to me since I got to move it off my dwindling-space C: drive

Collapse
 
mikekenneth77 profile image
Mike Houngbadji

Beautifully written and works like charm!

Collapse
 
sahasubzeus profile image
Subhojyoti Saha

Life saver! Clear and simple walkthrough!

12 Gorgeous UI Components for Your Design Inspiration

>> Check out this classic DEV post <<