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 on 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 command (WSL side):

# Linux Terminal
whoami
Enter fullscreen mode Exit fullscreen mode

who am I

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

#PowerShell
wsl --list --verbose
Enter fullscreen mode Exit fullscreen mode

wsl list

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

moving day

Move

All next steps 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 command.

# PowerShell
wsl --shutdown
Enter fullscreen mode Exit fullscreen mode

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

# PowerShell
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 finished, first of all, and more important ensure that the D:\backup\ubuntu.tarfile exists. Then we need to unregister the linux distribution from the 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!

# PowerShell
wsl --unregister Ubuntu
Enter fullscreen mode Exit fullscreen mode

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

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

Use the " (double-quote) character to surround the paths if they have blank spaces

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

# PowerShell
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 by running the command:

# PowerShell
wsl --distribution Ubuntu
Enter fullscreen mode Exit fullscreen mode

Recover

If something goes wrong in the process, don't worry, we can restore our WSL disk from the backup.

Didn't You skip the backup step? right?

right

Just repeat the steps after backup.


That’s All Folks!
Happy Coding 🖖

beer

Top comments (8)

Collapse
 
rudolphh profile image
Rudy A. Hernandez • Edited

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
 
equiman profile image
Camilo Martinez

I've updated the article to be more specific and avoid confusion with de default distribution.

Thanks!

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!

Collapse
 
marvinxu profile image
Marvin

I got stuck in the export step. doesn't it have an option to show progress?

Collapse
 
equiman profile image
Camilo Martinez

Sadly not, but I'll recommend you to run the npx npkill command and remove all the downloaded node_modules and also delete unnecessary files before making the backup. That will help the process to be shorter.

Collapse
 
marvinxu profile image
Marvin

Thanks for the tip! It's a very cool tool!