How to extract archives to file system in terminal?
I installed jupiter and I use the radiance theme so the icon doesnt look right, so I found this answer on how to change it How can I change the icon for Jupiter?. It mentions needing superuser access and I figured using sudo in terminal would be eaiser than enabling root or whatever I need to do, but I can't find anything on here that explains how to extract a tar.bz file to a directory in terminal.
Well i'm kinda new to linux, I wasn't sure if it was the right thing. And that's not working. I need to extract a few icons into the pixmap folder
-1: Questions this basic should be solved through a web search.
Actually, there is a little bit to think about here, to do this correctly. You have to make sure the files from the archive get to the right place, and have the right ownership. (`tar` will take care of the permissions, but not necessarily the ownership.) And it is not necessarily obvious to a novice which parts of the task need `sudo` and which do not. This seems like a valuable question for our site.
Fair enough. Truth be told I wasn't able to find a duplicate question on Ask Ubuntu itself so I see no problem with it being asked... I personally would try to at least come up with a candidate command by websearching or reading `man tar` before asking the question, but I do think this could be a valuable question for the site and I think I know who should write the answer... (nudge @EliahKagan, hehe)
Wisdom harbors questions, foolishness boasts answers. I'm glad you asked this. Thank you.
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 isjeff
, it is an abbreviation for/home/jeff
.)Now extract the archive with
tar
. Since that file is a.gz
archive, you'll use thez
flag to telltar
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 togunzip
it (as the.tar
archive is itself compressed withgzip
--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 oftar
, 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 arebolt1.png
,bolt2.png
, andbolt4.png
. So now, copy these files to/usr/share/pixmaps
. This is the part that requiresroot
privileges, so this is where you should usesudo
: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. Butroot
should own the files in/usr/share/pixmaps
, which is why you should use the--no-preserve=ownership
argument tocp
. Since you are copying the files asroot
in a directory owned byroot
, the copy you make will be owned byroot
as is proper.Method 2: Copying and Extracting the Archive as Root
You might find it simpler to do everything as
root
. Thenroot
will own the files initially, becauseroot
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 ofcp
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
asroot
. (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 asroot
, by running Nautilus (the file browser) asroot
. If you do this, you can perform any file management task with Nautilus, and any programs you launch from Nautilus will also run asroot
. 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 withsudo
), and because it would be particularly bad to forget that this Nautilus window was running asroot
rather than normally.To run graphical programs as
root
, don't usesudo
directly. Instead, usegksu
. So, to run Nautilus asroot
, 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 asroot
graphically, by running the Archive Manager asroot
. 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
, seeman tar
.If the archive had been
.tar.bz2
, you would usej
instead ofz
. If it had been.xz
, you would useJ
instead. For all other information, see that manual page.To learn more about performing administrative tasks in Ubuntu, see the community documentation on
sudo
androot
, and alsoman sudo
andman gksu
(orman 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 whattar
can and cannot do and how to use it. If this interests you, see the community documentation on backing up your system withtar
.+1, but can't you eliminate the `cp` step? something like `cd /usr/share/pixmaps` and then `sudo tar xzvf ~/Downloads/Jupiter_Radiance_theme_icons.tar.gz`?
I'll try the GUI based one, thank you. UPDATE: that method worked perfectly, thank you
@adempewolff That's true, this will work. I think the way I presented is conceptually similar...but I'll go ahead and add this way too, as it saves a good bit of typing and may be preferred.
I would agree that it's a tomatoes tom-_aw_-toes distinction. I'm just lazy about typing multiple commands!
Since I'm trying to make my answer a good presentation of the user's various options, I think it's a good idea for me to include this method. Thanks for suggesting it!
Specifically for extraction, is the gzip (z) flag _really_ necessary? I find that I can extract a tar.gz file just by using `tar xf archive.tar.gz`. It seems tar is smart enough to look at the file header to figure out what it needs to do to extract it. Also, did you know that programs run faster when stdout isn't bogging them down? It is for that reason that I personally avoid the verbose (v) flag.
@B1KMusic Right, you don't typically need `z`. Theoretically there could be corrupted archives that can be extracted (perhaps only partially) but only when you tell `tar` what kind of archive to try to extract, but in practice I think you're right: even corrupted archives likely won't benefit from that. As for performance and the `v` flag: I explain what the `v` flag does, people don't have to use it. Many-file archives--this situation where performance would decrease--would also produce rapidly scrolling, uninteresting output. Anytime someone really want to use `v`, slowdown is unlikely.
I just did some `time`s of `tar xf` vs `tar xvf` on a small archive, and without the v flag, tar had a 100ms headstart (xf = 0m0.013s; xvf = 0m0.114s). I imagine this would add up considerably for larger archives like X11, the SDL library, etc. Maybe I'm reading too deep into this, but I seem to notice faster 'untarring' speeds when excluding the v flag.
Here's something interesting: run `time 'yes >> file'` and kill it after ~1 second, it will print out a few million lines into the file in that one second it was running. Not only is this a testament to just how amazing computers are these days, but it really illustrates the delay between the CPU doing something and the result actually being pushed to stdout, which is apparently enough to stunt a few million lines of 'y' in 1 second to roughly a few thousand at best.
@B1KMusic I agree with everything you're saying, except the unstated subtext that any of this is relevant for `tar` on *small files*. In a situation where `tar` with `v` is so fast that no performance improvement is desired--as would be the case for any small archive--it simply doesn't matter that it runs X times as fast without `v`, no matter how big X is. Many novices are comforted by seeing output when they run commands they're not sure are correct because they're still learning to use them. The benefit of the `v` flag for user feedback is considerable.
@EliahKagan Yes, the difference is definitely negligible on files less than a few Megabytes. And either way, whether or not one wants to use the -v flag boils down to opinion/preference. In my case, for example, I'll run `tar xf`, and if it's a large tarball, switch to something else (e.g. grab a cup of water, watch some youtube, listen to some music) and check back every once in a while until the interactive prompt comes back, so for all practical purposes, the -v flag is pretty much useless to me. But that's just me. Generally, if I want to see the contents of a tarball, I'll use `tar tf`
License under CC-BY-SA with attribution
Content dated before 6/26/2020 9:53 AM
adempewolff 8 years ago
`sudo tar -xzvf example.tar.bz`, you really weren't able to find this by googling?! :P