Skip to content

tar

13 commands

Creates and extracts archives, with gzip, bzip2, xz or zstd compression.

  • archives
  • compression
  • backups

Install tar

sudo apt install tar

Creating6

tar -cvf <archive>.tar <paths>

Creates an uncompressed archive. c = create, v = verbose, f = file name.

Link
tar -czvf <archive>.tar.gz <paths>

gzip compression.

Link
tar -cJvf <archive>.tar.xz <paths>

xz compression: smaller but slower.

Link
tar --zstd -cvf <archive>.tar.zst <paths>

zstd compression: fast and small.

Link
tar -czvf <archive> --exclude=<pattern> <dir>

Leaves out matching paths.

Link
tar -czpf <archive> -C <dir> .

Archives a directory’s contents with relative paths and preserved permissions.

Link

Extracting4

tar -xvf <archive>

Extracts into the current directory. GNU tar detects compression automatically.

Link
tar -xzvf <archive> -C <dir>

Extracts into a specific directory.

Link
tar -xvf <archive> <path>

Extracts a single file.

Link
tar -xvf <archive> --strip-components=1

Drops the top-level directory from extracted paths.

Link

Inspecting3

tar -tvf <archive>

Lists contents without extracting. Always check archives from untrusted sources for ../ paths first.

Link
tar -df <archive>

Compares an archive against the file system and reports differences.

Link
tar -czf - <dir> | ssh <host> "tar -xzf - -C <dest>"

Streams a directory to another machine over SSH without a temporary file.

Link