Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Archives and Compression

Linux tar command

Reference for the Linux tar command: create, list and extract archives, compress with -z -j -J, use -C, exclude files, append, stream through pipes and extract safely.

tar bundles many files and directories, with their names, permissions, owners and timestamps, into a single archive file, and unpacks such archives again. It does no compression by itself, but it can hand the archive to gzip, bzip2, xz or zstd, which is how you get .tar.gz, .tar.bz2, .tar.xz and .tar.zst files.

You pick exactly one operation: create (-c), extract (-x), list (-t), append (-r), update (-u) or compare (-d). Everything else is a modifier. This page covers GNU tar, the version on almost every Linux system.

Terminal
tar -czf backup.tar.gz project/
tar -tf backup.tar.gz
tar -xzf backup.tar.gz -C /tmp/restore

Try it

Bash Create, list, extract and exclude

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Pipes, transforms and comparing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
tar -c [-f ARCHIVE] [OPTIONS] FILE...
tar -x [-f ARCHIVE] [OPTIONS] [MEMBER...]
tar -t [-f ARCHIVE] [OPTIONS]
Option Description
-c / -x / -t Create, extract, list.
-r / -u Append files / append only newer files (uncompressed archives only).
-d Compare archive with the file system.
-f FILE Archive name; -f - means standard input or output. Must be followed directly by the name.
-v Verbose: print each member.
-z, -j, -J, --zstd Filter through gzip, bzip2, xz, zstd. When reading, GNU tar detects the compression on its own.
-a Choose the compressor from the archive suffix when creating (.tar.xz).
-C DIR Change to DIR before processing the following names.
--exclude=PATTERN Skip matching names (glob; put before the file arguments).
-X FILE, -T FILE Read exclude patterns / names to include from a file.
-p Keep permissions exactly on extract (default for root).
--strip-components=N Drop N leading path parts on extract.
--transform=SED Rewrite member names with a sed expression.
--same-owner, --no-same-owner Restore or ignore recorded owners.
--one-file-system Do not cross into other mounted filesystems.
--overwrite, -k Overwrite existing files / keep existing files.
--wildcards Treat member names given on the command line as patterns.

Returns: 0 on success, 1 if some files changed while being read ("file changed as we read it"), 2 for fatal errors. A script should treat 1 as a warning for live data.

Usage notes

  • Decide what's inside. tar -cf a.tar dir stores paths starting with dir/; tar -cf a.tar -C dir . stores paths starting with ./ so extraction fills the current directory. Look first with tar -tf.
  • Leading / is removed. GNU tar strips it when creating ("Removing leading /' from member names") so extracting never writes to absolute paths by surprise. -P` disables this; avoid it.
  • Extract only trusted archives into a fresh directory. Archives can contain ../ paths or links pointing outside. Modern tar refuses most of these, but still list first with -t and extract with -C into an empty folder.
  • Order of options. -f takes the next argument; tar -czf out.tgz dir works, tar -cfz out.tgz dir creates a file named z.
  • Exclude before names. --exclude options apply to files named after them; put them before the paths.
  • Streaming. tar -cf - dir | ssh host 'tar -xf - -C /dst' copies a tree over the network preserving permissions; with -z you add compression.
  • Sparse and special files: -S stores sparse files efficiently; device nodes, links and extended attributes (--xattrs --acls) need those flags to be preserved.
  • Incremental backups: --listed-incremental=snapshot.snar makes level-0 and later archives that contain only changes.
  • Compression choice: gzip is fastest and universal, xz smallest but slower, zstd a good balance. See gzip, xz, zstd.
  • Verify what you wrote with -t and compare with sha256sum; a compressed archive is not readable if one byte is damaged in the middle.