How to extract archives to file system in terminal?

Solution 1:

Method 1: Extracting the Files, Then Copying Them as Root

Navigate in the Terminal to wherever the file is located. For example, if it's located in the Downloads folder inside your home folder, run this command:

cd ~/Downloads

The ~ character in this context is an abbreviation for the full name of your home folder. (For instance, if your username is jeff, it is an abbreviation for /home/jeff.)

Now extract the archive with tar. Since that file is a .gz archive, you'll use the z flag to tell tar this:

tar xvzf Jupiter_Radiance_theme_icons.tar.gz

x means to extract. v means to list the files as it extracts them (you can leave this off if you like). z means to gunzip it (as the .tar archive is itself compressed with gzip--that's what the .gz extension designates). f means to extract it here in the filesystem (and the need for it is an artifact of the older common use of tar, to create and extract tape archives).

The archive you just extracted contains three files (you saw their filenames if you kept the v flag in the command). Their names are bolt1.png, bolt2.png, and bolt4.png. So now, copy these files to /usr/share/pixmaps. This is the part that requires root privileges, so this is where you should use sudo:

sudo cp --no-preserve=ownership bolt1.png bolt2.png bolt4.png /usr/share/pixmaps

You had extracted them as your own (non-root) user, which gave you ownership over them. But root should own the files in /usr/share/pixmaps, which is why you should use the --no-preserve=ownership argument to cp. Since you are copying the files as root in a directory owned by root, the copy you make will be owned by root as is proper.

Method 2: Copying and Extracting the Archive as Root

You might find it simpler to do everything as root. Then root will own the files initially, because root will extract them. The easiest way to do this is to put the archive in the destination folder (if it's not already there).

Supposing the file is located in Downloads:

cd ~/Downloads
sudo cp Jupiter_Radiance_theme_icons.tar.gz /usr/share/pixmaps

Please note that you could instead have used mv instead of cp to move it instead of copying it (provided that the source and target folders are on the same partition).

Now go to the target folder and extract the archive:

cd /usr/share/pixmaps
sudo tar xzvf Jupiter_Radiance_theme_icons.tar.gz

You should probably remove the archive, because it's not good to have extraneous files in /usr/share/pixmaps:

sudo rm Jupiter_Radiance_theme_icons.tar.gz

Method 3: Just Extracting the Archive as Root

If you like, you can keep the archive wherever you downloaded it, and just extract it to /usr/share/pixmaps as root. (Thanks to adempewolff for suggesting I present this method.)

cd /usr/share/pixmaps
sudo tar xzvf ~/Downloads/Jupiter_Radiance_theme_icons.tar.gz

This works because tar will, by default, extract the archive to whatever folder you are in, rather than to the folder the archive is in (if they are different).

Other Methods

You can easily make a variation of Method 1 where you extract the files graphically with the Archive Manager, then copy them in the Terminal with sudo. But you can also do both as root, by running Nautilus (the file browser) as root. If you do this, you can perform any file management task with Nautilus, and any programs you launch from Nautilus will also run as root. You have to be careful with this, because you can damage your system by making a mistake (just as you can by running the wrong command with sudo), and because it would be particularly bad to forget that this Nautilus window was running as root rather than normally.

To run graphical programs as root, don't use sudo directly. Instead, use gksu. So, to run Nautilus as root, you could press Alt+F2 and run:

gksu nautilus

If you do this, make sure to close the Nautilus window when you're done, and to only use it for tasks where you know you need to be root (like making changes to the contents of /usr/share/pixmaps).

You could even do a variation of Method 2 or Method 3 where you don't copy anything as root, but instead extract the archive as root graphically, by running the Archive Manager as root. To do this, press Alt+F2 and run:

gksu file-roller

However, most users find it easier to extract files by launching the Archive Manager from within Nautilus, because then it opens knowing what archive you want it to use. (You can pass the name of the archive as part of the file-roller command...but at this point you start to lose the ease-of-use benefit of GUI over command-line.)

Suggested Resources

To learn more about extracting files with tar, see man tar.

If the archive had been .tar.bz2, you would use j instead of z. If it had been .xz, you would use J instead. For all other information, see that manual page.

To learn more about performing administrative tasks in Ubuntu, see the community documentation on sudo and root, and also man sudo and man gksu (or man kdesudo if you're using Kubuntu).

The community documentation on File Compression is worth a read, to learn more about archives and file compression. (Technically these are two related and overlapping but different things. For example: A .tar file is an archive. A .gz file is compressed.)

Most of the time you use tar it will probably not be to create and restore backups, but it can be useful for that, plus, understanding how that works enhances your understanding of what tar can and cannot do and how to use it. If this interests you, see the community documentation on backing up your system with tar.