2010/06/19

Archiving files

tar - archive utility (Tape ARchive).
  
For faster and more frequent use, gzip (Gnu ZIP) is generally a better choice but bzip2 can create a significantly smaller sized file. Using lzma (Lempel-Ziv-Markov chain Algorithm) provides even more compression but takes even more time than bzip2. Flags -v for verbose to show what is happening and -f for file since we're not actually using a tape drive in the '80s.
 
CREATE
tar cfv archive.tar file1 file2 ...
EXTRACT - Look at TF options below!
tar xvf archive.tar
COMPRESSION (BZIP2)
tar cfvj archive.tar.bz2 file1 file2 ...
COMPRESSION (GZIP)
tar cvfz archive.tar.gz file1 file2 ...
COMPRESSION (LZMA) - Don't forget the leading dash for standard options!
tar --lzma -cvfz archive.tar.lzma file1 file2 ...
LIST - Will indicate extraction paths that will be created!
tar tf archive.tar
PRESERVE - retain file permissions down to unique user and group IDs
tar cfvp archive.tar file1 ...
ABSOLUTE PATHS - Retain leading /
tar cfvP archive.tar /absolute/path/file1 ...
REMOVE FILES - Automatically delete original files after archiving
tar --remove-files -cvfz archive.tar.gz file1 file2 ...
 
There are also some other cool features worth mentioning: Files can be searched for within archives with the -t (or --list) flag by passing a second argument and additional files can be added to archives with the -r (--append or --update) flag and passing the filenames as additional arguments. Files in an archive can be replaced with current versions with -N (--newer), I believe, in the same way as used with -r.
 
This wonderful tool was originally implemented for use with archiving data to magnetic tapes. Appeared in Version 7 of Bell (AT&T?) Unix in 1979 (cool!) and one of my most favorite, often-used tools. Beyond the ability to package a multitude of files into one file, tar can also preserve file permissions, date and time information and directory structures.
 
The usual convention for file extensions for archived files are .tar.gz (.tgz), .tar.bz2 (.tbz or .tbz2 or .tb2) and .tar.lzma (.tlz). The gzip format has replaced the old .z archive (invoked with the -Z flag these days) format usually used by piping to the compress compression utility.

No comments:

Post a Comment