tar

Create, list and extract .tar, .tar.gz and other archives. One letter says what to do, and the others say how.

tar -xzf <archive>
Save

Angle brackets are yours to fill in. Windows, Linux, macOS.

Examples for tar

  • Extract a .tar.gz file

    tar -xzf <archive>.tar.gz
    Save

    x is extract, z is gzip, f names the file. Files go into the current directory. GNU tar and bsdtar detect the compression themselves, so tar -xf works for .tar.xz and .tar.bz2 too.

    tartool-xzfextract, gzip, this file<archive>.tar.gzpath
  • Compress a directory into a .tar.gz

    tar -czf <archive>.tar.gz <directory>
    Save

    c is create. The archive name comes right after f; the files and directories to include come last. The archive keeps the paths as you wrote them.

    tartool-czfcreate, gzip, this file<archive>.tar.gzthe new archive<directory>what goes in it
  • Extract into a specific directory

    tar -xzf <archive>.tar.gz -C <directory>
    Save

    The directory must already exist; tar does not create it. Make it first with mkdir -p.

    tartool-xzfflag<archive>.tar.gzpath-C <directory>extract here
  • See what is in an archive without extracting it

    tar -tzf <archive>.tar.gz
    Save

    Lists every path. Check this before extracting an archive from someone else: some put their files in a top-level directory and some do not.

    tartool-tzflist, gzip, this file<archive>.tar.gzpath
  • Leave files out of an archive

    tar -czf <archive>.tar.gz --exclude="node_modules" <directory>
    Save

    Put --exclude before the directory: some versions of tar ignore it when it comes after. Repeat it for each pattern.

    tartool-czfflag<archive>.tar.gzpath--exclude="node_modules"flag with value<directory>path
  • Extract only one file

    tar -xzf <archive>.tar.gz <path>
    Save

    Give the path exactly as tar -tzf lists it, including any top-level directory.

    tartool-xzfflag<archive>.tar.gzpath<path>as the archive lists it
  • Extract without the top-level directory

    tar -xzf <archive>.tar.gz --strip-components=1
    Save

    Drops the first directory from every path, so project-1.2/src/ is extracted as src/. Useful when you extract into a directory that already has the name you want.

    tartool-xzfflag<archive>.tar.gzpath--strip-components=1drop one directory

Flags

-c --create
Create a new archive.
-x --extract
Extract files from an archive.
-t --list
List what is in an archive without extracting it.
-f --file <archive>
The archive to read or write. Put it last in a group like -czf, because the next word is its value.
-z --gzip
Compress or decompress with gzip, for .tar.gz and .tgz.
-j --bzip2
Compress or decompress with bzip2, for .tar.bz2.
-J --xz
Compress or decompress with xz, for .tar.xz.
-v --verbose
Print each file name as it is processed.
-C --directory <dir>
Change to <dir> first. When extracting, this is where the files go.
--exclude <pattern>
Leave out files that match <pattern> when creating an archive.
--strip-components <n>
Remove the first <n> directories from each path when extracting.