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
  • 17,888
  • @Nmath Yah, I messed up earlier when I was updating versions. Pretty sure I unknowingly updated twice. See here for more context: https://askubuntu.com/questions/1450667/mismatch-between-my-terminal-vs-code-wsl-exe-ubuntu-version-18-04-and-etc-ls – coniferous Jan 20 '23 at 16:20
  • 1
    Can you add more details to your question what files you want to copy to the new system? Maxbe files in your home directory like documents? – Bodo Jan 20 '23 at 16:24

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
  • 17,888
  • 1
    +1 Clever, yet also safe, sensible, and straightforward. You found a way to un-scramble an egg. I doff my hat. – user535733 Jan 20 '23 at 18:01
  • Thank you so much @NotTheDr01ds... You've provided some much needed guidance during this kind of overwhelming process and really helped me not feel totally incompetent. – coniferous Jan 20 '23 at 18:53
  • @NotTheDr01ds can you explain (or point me in the direction of what to look up) how/why you're using the environment variable stuff ($env:...) when making and cd'ing into the new directory? Also, are you on discord? – coniferous Jan 23 '23 at 15:44
  • @coniferous Well, I'm on Discord, but I often forget to launch it ;-). I'm here on Stack far more. You can join me in chat. I'll start typing up the $env explanation there. – NotTheDr01ds Jan 23 '23 at 19:20
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
  • 380
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
  • 300