Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Some introductory tips on using 'tar' compression UNIX
I have been an off & on *nix user since the early 90s. I have used various versions, but only sporadically. It has been over four years since I used *nix with any regularity and I love having the ability in MacOSX.

I gleaned this info many years ago from various sources. Unfortunately I don't know who all contributed. I simply made myself a text file called "tgz notes" and have kept it all these years for those times when I couldn't remember how I did this.

Read the rest of the article for some different examples of how to use 'tar'.

To tar the current directory into one file and save the new file in the parent directory:
 % tar cf - . > ../filename.tar
To then zip the new file:
 % cat ../filename.tar | gzip > ../filename.tgz
To do both at the same time:
 % tar cf - . | gzip > ../filename.tgz
To extract, first change to the location you want the files (ie. create the outermost directory of the archive and then cd to it) and then:
 % zcat /some/path/filename.tgz | tar xvf -
So to archive the entire file structure within a directory:
 % cd /some/path/directory
% tar cf - . | gzip > ../directory.tgz
You end up with a new file named /some/path/directory.tgz. Then you do the following to put the directory structure somewhere else:
 % cd /new/location
% mkdir new_directory
% cd directory
% zcat /some/path/directory.tgz | tar xvf -
And you end up with the identical structure at both places, /some/path/directory and /new/location/directory.

[Editor's note: Although basic for experienced UNIX users, these tar tips should be helpful to those new to this world on the Mac.]
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[5,211 views]  

Some introductory tips on using 'tar' compression | 11 comments | Create New Account
Click here to return to the 'Some introductory tips on using 'tar' compression' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Convoluted
Authored by: kwalker on May 16, '02 09:28:26AM

These tips are very convoluted. There are more
direct ways to do it:

>To tar the current directory into one file and save the new
>file in the parent directory:
>
> % tar cf - . > ../filename.tar

Better: % tar cf ../filename.tar .

>To then zip the new file:
>
> % cat ../filename.tar | gzip > ../filename.tgz

Better: % gzip filename.tar

>To do both at the same time:
>
> % tar cf - . | gzip > ../filename.tgz

Better: % tar czf ../filename.tgz .



[ Reply to This | # ]
-z flag for tar
Authored by: miketja on May 16, '02 09:34:23AM

The tar command on OSX includes the option for the '-z' flag, which will (de)compress the tar file on the fly -- no need to use stdin/stdout piped to gzip. For example:

% tar cf - . | gzip > ../filename.tgz

can be shortend to:
% tar zcf - . > ../filename.tgz

Also, the '-' after the 'f' option tells tar to send its output to STDOUT, but this could also be a file, eg.
% tar zcf ../filename.tgz .

Both these options also work in reverse:
% tar zxf ../filename.tgz
will gunzip the file on the fly as well as extracting the files from the tar archive.



[ Reply to This | # ]
Even better
Authored by: Anonymous on May 16, '02 09:52:44AM
Even better:

unix% man tar

Giving convoluted and/or incorrect examples (thanks to the first followup post for better tar usage) as an introduction to a specific UNIX command isn't a complete tip. It would help to include information on where to go to find more information. The UNIX manual pages are the probably the best ways to find out what UNIX commands are, what they do, and how they work. For an introduction to the UNIX manual system, try this command:

unix% man man


[ Reply to This | # ]
Re: Even better
Authored by: kwalker on May 16, '02 10:09:58AM

To be fair, though, many of the unix man pages (including the tar man page) are terse and could benefit from some examples.



[ Reply to This | # ]
Re: Even better
Authored by: Anonymous on May 16, '02 11:27:37AM
Absolutely! The 'tar' examples posted were just bad examples. The man pages do contain good info about additional options, etc. If someone who was not a strong UNIX user didn't know about the 'tar' man page, they might never know what else 'tar' could do. That's why I thought it should have been mentioned. Examples are probably the best initial learning tool, still.

[ Reply to This | # ]
re man
Authored by: brownpw on May 17, '02 01:03:50AM

Dud, have you tried to read the man entry for tar? Maybe it's just the ADHD , but I have neither the desire or the time. For *real* world help on any command, you should always start with the -help option, almost every normally used command has one. For this example, you could just use:

% tar -help

which will produce something that you can actually *use*:

[localhost:~] brownpw% tar -help
tar: illegal option -- l
usage: tar -{txru}[cevfbmopswzBHLPXZ014578] [tapefile] [blocksize] [replstr] [-C directory] file1 file2...

Notice that even though the tar command doesn't have the -help option available to the user, it still fails over to give you the info you need. Some would suggest that this is bad form, but my boss would suggest that I get my work done and not waist all day reading overly confusing and exhaustive man pages, like the one that is given for tar.



[ Reply to This | # ]
Backup with tar
Authored by: bluehz on May 16, '02 02:08:21PM

I have been delving into the Linux world on an old dumpster PC I saved. I noticed that Linux people use tar as a means of backing up. There are numerous scripts and examples if you look around the Linux community. Many of them sophisticated enough to maintain incremental backups in one tar file. Just something to think about - when you star realizing just how much more capable tar is than we think.

Also - give willie a break...at least he brought up the topic and started this discussion. Did you do that????

No one should be chided for trying to teach others - thats what we are all here for - its a community - I learn from you and you learn from me. Many times I may not know how to do something or believe I know how to do something when I realy don't. Through the gracious users at MacOSXHints - I have been taught the right ways and what was wrong with the way I was doing it. That doesn't make me stupid or an idiot - it just means I (like most of us that come here) have a lot to learn. So please - if you know a better way of doing something - use a little grace and tact in informing the rest of us - no need for name calling.



[ Reply to This | # ]
There's more than one way to do it
Authored by: Chas on May 16, '02 02:48:37PM

Yes, this is the Unix philosophy, there's always more than one way to do things. There are compressed, more cryptic methods for people with deep Unix knowledge. But the command line is pretty comprehensible even for newbies, they may not do things the simplest or most straightforward way, but they get the job done.
But I do have to warn people about tar, remember it's designed for Unix files, and may not properly compress Mac files with resource forks. Proceed with caution and always test backups by restoring them to a different directory, and see if the restores work.



[ Reply to This | # ]
There's more than one way to do it
Authored by: gvitale on May 17, '02 04:31:56AM

As for the ability to archive HFS files (with resouce fork and finder info) someone (http://www.geocities.com/paulotex/tar/) has pached GNU tar to work with HFS files. I use it and it works!



[ Reply to This | # ]
Download and decompress in one hit
Authored by: calroth on May 23, '02 03:41:54AM

Here's a cool way to use tar:

curl http://some.server/download.tar.gz | tar xvfz -

Downloads the file and decompresses it in one go. Similarly for a straight gzip file, although it decompresses to standard output by default so you have to redirect it to a file.

curl http://some.server/download2.txt.gz | gunzip > download2.txt



[ Reply to This | # ]
Some introductory tips on using 'tar' compression
Authored by: cyberRodent on Apr 15, '03 02:11:24PM

I have a question I hope someone can help -- my tgz files created from tar cfz
archive.tgz directory -- are more than twice as big as the original directory --
an 80MB directory turns into a 200MB tgz !!! -- decompressing the 200MB
tgz results in the 80MB directory -- what gives? any ideas? please reply to
tar @ cyberRodent dot com.

Thanks...
(os 10.2.5) command I'm using in a bash script...
/usr/bin/tar -czf `echo $OLDMIRROR` ./MIRROR



[ Reply to This | # ]