5 ms·
> Extract tar.gz in new directory: tar zxvf package.tar.gz -C new_dir For quick reference, since it seems to be hard[1], these are the flags you'll most need i
by lucb1e 13y ago
> Extract tar.gz in new directory: tar zxvf package.tar.gz -C new_dir
For quick reference, since it seems to be hard[1], these are the flags you'll most need in tar:
c = create
x = extract
v = verbose
z = gzipped; compressed.
f = file
So for example `tar xf example.tar` will extract that tar file. Or `tar cf example.tar .` will pack all files in the current directory to example.tar. For compression and verbosity, it would be `tar zvcf example.tar.gz .`. The .gz at the end is optional, but now others can see what kind of archive it is. Extract .tar.gz files with `tar xzf example.tar.gz`.
For bonus points, here is a poor man's version of scp/rsync:
# sending computer
tar czf - . | openssl aes-128-cbc -k 'secret' | nc -l 1
# receiving computer
nc 192.168.1.102 1 | openssl aes-128-cbc -d -k 'secret' | tar xf -
[1] Obligatory xkcd https://xkcd.com/1168/ https://xkcd.com/1168/
Edit: replaced all asterisks with a period (.) since it would become italics. Tar is recursive by default, so it should work the same.
- dmd 13y agoAnd the 'z' flag is unnecessary; gnu tar is smart enough to figure that out on its own.
- lucb1e 13y agoYesterday I got an error 'error: tar archive is compressed. Aborting' or something along those lines. When reading a file it might detect it because of the filename, but with stdin you need to specify the flag it seems.
- parennoob 13y agoJust use dtrx (do the right extraction) via `pip install dtrx`. Makes life much easier.
- voltagex_ 13y agoGNU tar seems to have gained the a switch which will detect the right decompressor. e.g. tar axf linux.tar.gz tar axf linux.tar.bz2 tar axf linux.tar.xz will all work, assuming you have gzip, bzip2 and xz installed.
- deathanatos 13y agoNowadays, `tar -xf foo.tar.gz` (at least in Linux) will auto-detect the gzipping and just do the right thing. I think it uses file extension, so I'd suspect it works for .bz2 and .xz too. (Not sure I'd use this for shell scripts, but for everyday command line it should work.)
- deleted 13y ago[deleted]
- sobering 13y agoI'd also add `-j' to that list. j = bzip2 I find a lot of packages with the `.tar.bz2' extension. Just replace `z' with `j' in your tar command and you're good.
- lucb1e 13y agoCan't edit my post anymore, but good tip! As dmd commented though, it seems tar automatically detects compression on files because of the file extension.
- fit2rule 13y agoCheese note: I still do the ol' pipe dance when de-compressing archives, like this: gunzip -c somefile.tar.gz | tar xvf - The reason? Well, its like this: I learned it that way on machines that had, typically, about 4 megs available. So it used to be, back in the ol' days, that if you tried to do it like: tar xvzf somefile.tar.gz .. you'd run out of RAM, get into swapfile sadness, and so on. Piping it through the console first, used the tty-buffers, which blocked whenever exhaustion occurs, but nevertheless: recovered a lot smoother than when heavy swap was involved .. Dunno if its still 'true' (could be a mythos I'm passing on from the old, drunk sysadmin I learned the rule from) but it aways bugs me when I forget to do it 'the new way' ..
- jlgaddis 13y ago> I still do the ol' pipe dance when de-compressing archives, like this: Same here. Old habits die hard, I suppose.
- bediger4000 13y agoThis is at least sometimes still true in corporate Linux environments. Giant, Immoral MegaCorps apparently can't be bothered to spring for big disks, so doing "tar xzf" or gunzipping something before un-tarring often fills up the filesystem. Embarassing, but true, given that you can hoof it to Office Depot and buy terabytes for a couple hundred, if they'd just let you.