tar Commands

tar commands cheatsheet.

Create, extract and inspect tar archives without memorising the flags. Tap any command to copy it.

Create archives
tar -cvf archive.tar files/Create an uncompressed tar archive
tar -czvf archive.tar.gz files/Create a gzip-compressed archive
tar -cjvf archive.tar.bz2 files/Create a bzip2-compressed archive
tar -cJvf archive.tar.xz files/Create an xz-compressed archive
tar -czvf a.tar.gz --exclude='*.log' dir/Create, excluding a pattern
Extract archives
tar -xvf archive.tarExtract a tar archive
tar -xzvf archive.tar.gzExtract a gzip archive (.tgz too)
tar -xjvf archive.tar.bz2Extract a bzip2 archive
tar -xvf archive.tar -C /path/to/dirExtract into a specific directory
tar -xvf archive.tar file.txtExtract a single file from the archive
List & inspect
tar -tvf archive.tarList contents without extracting
tar -tzvf archive.tar.gzList a gzip archive's contents
tar -rvf archive.tar newfile.txtAppend a file to an existing tar
Advanced
tar -czvf - dir/ | ssh host 'cat > a.tgz'Stream a compressed archive over SSH
tar -czf - dir/ | wc -cGet the compressed size without saving
tar --strip-components=1 -xzvf a.tgzExtract, dropping the top folder level
Flag memory aid: c = create, x = extract, t = list, v = verbose, f = file (always last, before the filename), z = gzip, j = bzip2, J = xz.

The three jobs: create, extract, list

Despite its long list of options, tar really does three things, and the flags spell them out. tar -cvf creates an archive, tar -xvf extracts one, and tar -tvf lists what's inside without unpacking it. The f always comes last in that cluster because it introduces the filename. Get those three muscle-memorised and you've covered almost everything you'll ever do with tar.

Compression is just an extra letter

A plain .tar bundles files together without shrinking them. Adding z pipes it through gzip (the ubiquitous .tar.gz, also written .tgz), j uses bzip2 for smaller-but-slower .tar.bz2, and J uses xz for the smallest .tar.xz. The same letter that created the archive must be used to extract it, which is why tar -xzvf is probably the single most-typed tar command in existence. Modern GNU tar can often auto-detect the compression on extract, but spelling it out never hurts.

Handy extras worth knowing

Two options save real time. -C /path changes directory before acting, so you can extract an archive straight into the folder you want instead of moving files afterwards. --exclude='pattern' leaves matching files out when creating an archive — perfect for skipping logs or a node_modules folder. And because tar can write to standard output with -f -, you can pipe an archive directly over SSH to back up a directory to another machine in one line. Pair this with the rsync commands for ongoing syncs and the SSH commands for the remote connection.

FAQ

How do I extract a .tar.gz file?
Use tar -xzvf file.tar.gz. The x extracts, z handles the gzip compression, v lists files as they're extracted, and f points at the file. Add -C /path to extract somewhere specific.
What do the tar flags c, x, z, v and f mean?
c creates an archive, x extracts one, t lists contents; z adds gzip compression; v is verbose (show files); and f means the next argument is the filename. f goes last in the flag cluster.

More cheatsheets