My blog has moved!

You should be automatically redirected. If not, visit
http://benohead.com
and update your bookmarks.

Thursday, November 17, 2011

Tar

tar (Tape ARchiver) is a Linux command to archive files and directories. It can be used to create an archive file and optionally compress it (using gzip or bzip2)

Usage

Create an archive file
tar cvf archive.tar directory/
The c option will have tar create a new archive file. The following additional options are usually also use:
    v verbose output (list files processed)
    f followed by the path to the archive file to be created (if omitted, the standard output is used - which only makes sense if you redirect it)

The last argument is a list of files/directories to be archived e.g. tar cvg archive.tar dir1 dir2 file3 file4

The files are added to the archive including the directory structure.

Additionally the archive file can be compressed:

Using gzip:

tar cvzf archive.tar.gz directory/
tar cvzf archive.tgz directory/
(.tgz is an extension often used instead of .tar.gz).

Using bzip2:
tar cvjf archive.tar.bz2 directory/

Update an archive file
tar uvf archive.tar directory/
With the u option, tar will update the referenced archive file adding (or updating if already contained) the provided files.

Extract an archive file
tar xvf archive.tar  
The x option will have tar extract the contents of the referenced archive file to the current directory.

List the contents of an archive file
tar tvf archive.tar
The t option will have tar list the files contained in the referenced archive file.

Alternatively, less can also be used to list the contents of a TAR archive:
less archive.tar

Tips

Archive on floppy
 
tar cMvf /dev/fd0 /pfad
/dev/fd0 is the device of the floppy drive.
The M option tells tar to create a multi-volume archive. This means that if the archive is too big for a single floppy it will be split to multiple floppies.

Similarly, you can use the other options above instead of c (to update, list or extract).

Get the list of files to archive from a file
 
tar cvf archive.tar -T tar.list
where tar.list contains the list of files to be added to the archive.

Archive only a subset of the files 
find . -name "*.log" -print | tar cvf archive.tar -T-
This will look recursively for all *.log files and add them to the archive.

-T- tells tar not to use a file as input but the standard input (which in this case is a redirect of find's output).

Exclude invidual files 
tar cvf archive.tar -X exclude.list directory/
The -X option is followed by the path to a path containing a list of files which should not be included in the archive.

Alternatively a pattern can be used to exclude files:
tar cvf archive.tar --exclude=*.log directory/  
This will archive all files and subdirectory in "directory" except log files.

1 comment:

  1. The blog was absolutely fantastic! Lots of great information and
    inspiration, both of which we all need!b Keep 'em coming... you all do
    such a great job at such Concepts... can't tell you how much I, for
    one appreciate all you do!

    ReplyDelete