How to Compress and Extract Files with tar

Published 2026-08-05 · Learn Linux

The tar command packs files into a single archive and, with the right flags, compresses it. tar -czf backup.tar.gz project/ creates a compressed archive, and tar -xzf backup.tar.gz unpacks it. Those two commands handle most archiving needs.

Create a compressed archive

The flag letters are a compact recipe: c create, z gzip-compress, f write to a file (required). The archive name comes first, then the files or folders to include.

$ tar -czf backup.tar.gz project/

The .tar.gz ending is the standard convention; you can name the archive anything, but keeping the extension avoids confusion.

List the contents without extracting

Before unpacking an unfamiliar archive, list what is inside with t for table of contents. You can search the listing with grep.

$ tar -tzf backup.tar.gz
$ tar -tzf backup.tar.gz | grep docs

Extract an archive

Use x for extract. tar writes the files into your current directory, recreating the folder structure stored in the archive. List the contents first if you are not sure where they will land.

$ tar -xzf backup.tar.gz

If you are not using gzip (a plain .tar file), drop the z: tar -xf archive.tar.

Extract to a specific folder

To unpack somewhere other than the current directory, pass -C with the target path. The folder must already exist.

$ tar -xzf backup.tar.gz -C /tmp/restore

Add progress and common extras

Use -v (verbose) to print each file as it is processed, and --exclude to leave files out when creating an archive.

$ tar -czvf backup.tar.gz project/ --exclude=project/node_modules

Modern tar also reads .tar.xz and .tar.bz2 automatically. The tar reference page documents everything else.

Keep learning

Related guides: How to Copy Files and Directories with the Linux cp Command · How to Sync Files with rsync · How to Check Disk Usage with df and du. Read all tutorials on the Learn Linux blog.