Differences between revisions 4 and 5
Revision 4 as of 2022-09-27 01:54:23
Size: 747
Comment:
Revision 5 as of 2022-09-27 02:59:02
Size: 4068
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from CheatsheetCompression
= Compression/Decompression =
= Tar =

'''`tar(1)`''' is the '''t'''ape '''ar'''chive utility. It creates '''tarballs''' that may or may not be compressed and/or encrypted.

There are multiple implementations:

 * [[Busybox]] implements POSIX `tar(1)`
 * '''`paxtar(1)`''' which all BSDs (other than [[BSD/FreeBSD|FreeBSD]]) use
 * '''GNU `tar(1)`''' (or '''`gtar(1)`''') is an extended implementation
 * '''`bsdtar(1)`''', which is built on `libarchive`, is a further extended implementation
   * many Linux distributions will ship ''even further'' extensions by way of compilation options for `libarchive`
     * for example, `zstd(1)` support
Line 10: Line 20:
== tar == == Installation ==
Line 12: Line 22:
=== Create === `tar(1)` is POSIX and will already be installed.
Line 14: Line 24:
{{{
tar -cvf archive.tar file1 file2 file3
}}}
If running a Linux distribution that uses `gtar(1)` by default, `paxtar` and `bsdtar` packages will be available.
Line 18: Line 26:
 * `c` for create
 * `f` for output filename
 * `v` for verbose
If running a BSD distribution that uses `paxtar(1)` by default, `gtar` and `bsdtar` packages ''may'' be available.

If running a BSD or Linux distribution that uses `bsdtar(1)` by default, a `gtar` package ''may'' be available. [[Linux/ArchLinux|Arch]] does actually provide a `paxtar` package as well, but this is uncommon.

----
Line 24: Line 34:
=== Create gzip-compressed === == Syntax Across Implementations ==

POSIX `tar(1)` uses a '''bundled-argument''' format. All flags are bundled into the first argument.
Line 27: Line 39:
tar -czvf archive.tar.gz file1 file2 file3 tar cvf output.tar input-file-list
Line 30: Line 42:
 * `z` for gzip compression 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."

----
Line 34: Line 70:
=== Create gzip-compressed, password-protected === == Usage ==

Create a tarball.
Line 37: Line 75:
tar -czvf archive.tar.gz file1 file2
gpg
-c archive.tar.gz
tar --create --file archive.tar -- file1 file2 file3
Line 41: Line 78:


=== Extract all ===
Create a `gzip(1)`-compressed tarball.
Line 46: Line 81:
tar -xvf archive.tar tar --create --gzip --file archive.tar.gz -- file1 file2 file3
Line 49: Line 84:
 * `x` for extract Compression options:
Line 51: Line 86:
 * `-z` and `--gzip` for `gzip(1)`
 * `-j` and `--bzip2` for `bzip2(1)`
 * `-J` and `--xz` for `xz(1)`
 * `--auto-compress` to determine compression algorithm from the file extension
   * ''this is a `bsdtar(1)` extension''
 * `--zstd` for `zstd(1)`
   * ''this is a `bsdtar(1)` extension that is only available on some BSD and Linux distributions''
 * `--lzip` for `lzip(1)`, `--lzma` for `lzma(1)`, `--lzop` for `lzop(1)`, and `-Z`/`--compress` for `compress(1)`
   * ''don't use these''
Line 52: Line 96:

=== List all ===
Create a `gzip(1)`-compressed, `gpg(1)`-encrypted tarball.
Line 56: Line 99:
tar -tf archive.tar tar --compress --gzip -- file1 file2 | gpg --symmetric --output archive.tar.gz.gpg
Line 59: Line 102:
 * `t` for something... I'm not actually sure what 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
}}}

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:

  • Busybox implements POSIX tar(1)

  • paxtar(1) which all BSDs (other than FreeBSD) use

  • GNU tar(1) (or gtar(1)) is an extended implementation

  • bsdtar(1), which is built on libarchive, is a further extended implementation

    • many Linux distributions will ship even further extensions by way of compilation options for libarchive

      • for example, zstd(1) support


Installation

tar(1) is POSIX and will already be installed.

If running a Linux distribution that uses gtar(1) by default, paxtar and bsdtar packages will be available.

If running a BSD distribution that uses paxtar(1) by default, gtar and bsdtar packages may be available.

If running a BSD or Linux distribution that uses bsdtar(1) by default, a gtar package may be available. Arch does actually provide a paxtar package as well, but this is uncommon.


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:

  • -z and --gzip for gzip(1)

  • -j and --bzip2 for bzip2(1)

  • -J and --xz for xz(1)

  • --auto-compress to determine compression algorithm from the file extension

    • this is a bsdtar(1) extension

  • --zstd for zstd(1)

    • this is a bsdtar(1) extension that is only available on some BSD and Linux distributions

  • --lzip for lzip(1), --lzma for lzma(1), --lzop for lzop(1), and -Z/--compress for compress(1)

    • don't use these

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


CategoryRicottone

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