Create, extract and inspect tar archives without memorising the flags. Tap any command to copy it.
tar -cvf archive.tar files/Create an uncompressed tar archivetar -czvf archive.tar.gz files/Create a gzip-compressed archivetar -cjvf archive.tar.bz2 files/Create a bzip2-compressed archivetar -cJvf archive.tar.xz files/Create an xz-compressed archivetar -czvf a.tar.gz --exclude='*.log' dir/Create, excluding a patterntar -xvf archive.tarExtract a tar archivetar -xzvf archive.tar.gzExtract a gzip archive (.tgz too)tar -xjvf archive.tar.bz2Extract a bzip2 archivetar -xvf archive.tar -C /path/to/dirExtract into a specific directorytar -xvf archive.tar file.txtExtract a single file from the archivetar -tvf archive.tarList contents without extractingtar -tzvf archive.tar.gzList a gzip archive's contentstar -rvf archive.tar newfile.txtAppend a file to an existing tartar -czvf - dir/ | ssh host 'cat > a.tgz'Stream a compressed archive over SSHtar -czf - dir/ | wc -cGet the compressed size without savingtar --strip-components=1 -xzvf a.tgzExtract, dropping the top folder levelDespite 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.
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.
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.