4

I am updating my Ubuntu distribution from 18.04 to 22.04. I exported the Ubuntu 18.04 instance with wsl --export Ubuntu-18.04 <file.tar> to a tarball and am trying to figure out how to effectively extract the files I need from it into my new 22.04 instance.

I know I can...

  • view the contents of the tarball with tar -tvf <file.tar> (t: view contents, v: verbose, f: next arg is filename)
  • extract the whole thing with tar -xvf <file.tar> (x: extract)
  • extract a file or folder with tar -xvf <file.tar> <filename_or_folder>

But I probably have over 1k files when I list them and I figure there has to be a more efficient way to go through them than listing and manually reading over ALL the files. I'm honestly not sure what all is in there, but I'm at least wanting to move over the relevant files for python and java projects and any config files.

I have already run the following, so using a different approach might be hard at this point:

  • wsl --terminate Ubuntu-18.04
  • wsl --unregister Ubuntu-18.04
NotTheDr01ds
  • 22,082

3 Answers3

5
First, an answer to the question itself (but see below for a potentially better option)

I would start by doing a wsl --import into a new (temporary) distribution.

Similar to my other answer, start in PowerShell and create a new directory for the temporary distribution:

mkdir "$env:USERPROFILE\wsl\coniferous_ubuntu_old"
cd "$env:USERPROFILE\wsl\coniferous_ubuntu_old"

Then:

wsl --import coniferous_ubuntu_old "$env:USERPROFILE\wsl\coniferous_ubuntu_old" <path_to_tarball> --version 2
  • The first argument to --import is the name of the new distribution.
  • Second is the directory for the new distribution
  • Third is the tarball created from the --export
  • Finally, --version 2 is just a good practice to make sure that WSL2 is used.

With that in place, see my answer on Super User for information on accessing the files in one distribution from the other. Option #1 is still preferred.

(Possible) Preferred Alternative

I'd probably just delete your new 22.04 distribution if you haven't done too much with it (using --unregister) and --import the old one back in as mentioned above.

This would ultimately have the same end result as this answer that you missed. You could then do the upgrade from 18.04 to 20.04, then 20.04 to 22.04. (Although it sounds like there's a chance that already completed, even).

Naming the distribution without a version number, again, will correct your original problem.

Notes on other alternatives

If you extract the tarball of the old distribution inside the new one, then keep in mind that the size will grow quite a bit. WSL2 distributions will grow in side to accommodate the files inside, but do not (automatically) shrink when the files are deleted.

NotTheDr01ds
  • 22,082
2

"The relevant files for python and java projects and any config files" might all be in your home directory, but that depends on how you used your system.

Config files should be compared with the new versions, not copied without check, because a new version of a program might need new settings that were not present (not valid) in the old version.

You could write the archive listing to a file and optionally filter the output, e.g.

tar -tf <file.tar> | fgrep '/home/' > <file-of-names>

or

tar -tf <file.tar> > <file-of-names>

Check if your archive contains absolute or relative paths. You might have to use home/ instead of /home/.

Then you can review and modify the file name list using a text editor that preserves the UNIX line endings, leaving only the files you want.

With GNU tar, which should be the default on Ubuntu (and most Linux distributions), you can specify the files you want to extract to be read from a file, e.g.

tar -T <file-of-names> -xvf <file.tar>
Bodo
  • 591
1

[Partial answer]

Regarding the extraction of desired files, you can install Midnight Commander (apt install mc), a console file manager simple but with many functions.

You can open the tar file in one panel simply by selecting it and pressing [Enter], and navigate/select files inside, then select in the other panel the destination and copy/move/read to your heart's content.

It has online help pressing [F1], menu commands pressing [F9], etc., and you can consult man mc (also its integrated editor mcedit seems to me more user friendly than nano/vi and the like and it is callable outside mc). It is very configurable (colors, menu options, macros, etc.)

Fjor
  • 314
  • 2
  • 10