Move WSL File System to another Drive
Tags: terminal, wsl, linux, ubuntu
Published: 2021-11-27 00:14:32

Language: [🇪🇸] [Español](https://dev.to/equimancho/mover-el-sistema-de-archivos-de-wsl-a-otro-disco-3fbi) - [🇺🇸] [English](https://dev.to/equiman/move-wsl-file-system-to-another-drive-2a3d)

---

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):

```bash
# Linux Terminal
whoami
```

![who am I](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6kvdb7puzscbb96q0k5.png)

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

```bash
#PowerShell
wsl --list --verbose
```

![wsl list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2xke436r85ci0t2aqgs.png)

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

![moving day](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9luo23t87clkxk1wzhpx.gif)

##  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.

```bash
# PowerShell
wsl --shutdown
```

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

```bash
# PowerShell
mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inlo80w5ccpkaxjcnv4d.gif)

When finished, first of all, and more important **ensure** that the `D:\backup\ubuntu.tar`file 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!

```bash
# PowerShell
wsl --unregister Ubuntu
```

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

```bash
# PowerShell
mkdir D:\WSL
wsl --import Ubuntu D:\WSL\ D:\backup\ubuntu.tar
```

> 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.

```bash
# PowerShell
cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
ubuntu config --default-user equiman
```

> Ensure replacing `equiman` with yours.

Restart WSL by running the command:

```bash
# PowerShell
wsl --distribution Ubuntu
```

# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t27z7tqemo0j75hh8ayw.gif)

Just repeat the steps after backup.

---

**That’s All Folks!**
**Happy Coding** 🖖

[![beer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgsx4aw6qn9i9ax6as9.png)](https://github.com/sponsors/deinsoftware)