Tar

tar(1) is the tape archive utility. It creates tarballs that may or may not be compressed and/or encrypted.

There are multiple implementations:


Installation

All Linux and BSD distributions will have tar(1) installed, though the implementation will differ.

Arch Linux uses bsdtar(1), but packages for all other tar(1) implementations are available.

gtar and bsdtar packages are the most widely available.


Syntax Across Implementations

POSIX tar(1) uses a bundled-argument format. All flags are bundled into the first argument.

tar cvf output.tar input-file-list

It should be immediately evident that this is a strange syntax.

The Unix format bridged the gap between this a more normal syntax.

tar -cvf output.tar input-file-list
#or
tar -c -v -f output.tar input-file-list

Each flag or cluster of flags is prefixed with -. If a flag that takes an argument is in a cluster, it must be at the end. Mandatory arguments can either be the following word (-f output.tar) or fused to the flag (-foutput.tar). A frequent tripping point: optional arguments must be fused to the flag (-g/tmp/snar.db).

The GNU format extended this with much-beloved syntactic sugar: long options.

tar --create --verbose --file output.tar input-file-list

Additionally, mandatory options can now be any of: the following word (--file output.tar), fused to the flag (--fileoutput.tar), or joined to the flag by = (--file=output.tar).

bsdtar(1) is compatible with all formats and brings additional features (automatic discovery of compression algorithms, handling of sparse files, etc.).

Per the manual of bsdtar(1): "For maximum portability, scripts that invoke tar should use the bundled-argument format above, should limit themselves to the c, t, and x modes, and the b, f, m, v, and w options."


Usage

Create a tarball.

tar --create --file archive.tar -- file1 file2 file3

Create a gzip(1)-compressed tarball.

tar --create --gzip --file archive.tar.gz -- file1 file2 file3

Compression options:

Create a gzip(1)-compressed, gpg(1)-encrypted tarball.

tar --compress --gzip -- file1 file2 | gpg --symmetric --output archive.tar.gz.gpg

Extract the contents of a tarball.

tar --extract --file archive.tar

Extract the contents of a gzip(1)-compressed tarball.

tar --extract --gzip --file archive.tar.gz

Extract the contents of a gzip(1)-compressed, gpg(1)-encrypted tarball.

gpg --decrypt archive.tar.gz.gpg | tar --extract --gzip

List the contents of a tarball.

tar --list --file archive.tar


See also

tar(1)

bsdtar(1)


CategoryRicottone

Tar (last edited 2023-04-08 13:33:22 by DominicRicottone)