How to Compress and Extract Files with tar
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.
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.
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.
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.
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.
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.