Skip to main content

tar Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for creating, listing, extracting, and compressing tar archives in Linux

Tar bundles files and directories into archive files, and it is commonly used with gzip, bzip2, or xz compression. This cheatsheet covers practical tar command patterns for backup, restore, and archive inspection.

Basic Syntax

Core tar command forms.

CommandDescription
tar -cf archive.tar files/Create archive
tar -xf archive.tarExtract archive
tar -tf archive.tarList archive contents
tar -rf archive.tar file.txtAppend file to archive (uncompressed only)
tar --delete -f archive.tar file.txtRemove file from archive (uncompressed only)

Create Archives

Common archive creation patterns.

CommandDescription
tar -cf backup.tar /etc /var/wwwArchive multiple paths
tar -cf project.tar project/Archive a directory
tar -cvf project.tar project/Verbose create
tar -cf archive.tar --files-from=filelist.txtCreate from file list
tar -cf archive.tar --exclude="*.log" app/Exclude pattern while creating

Extract Archives

Extract all files or selected paths.

CommandDescription
tar -xf archive.tarExtract in current directory
tar -xvf archive.tarVerbose extraction
tar -xf archive.tar -C /tmp/restoreExtract to target directory
tar -xf archive.tar path/in/archive.txtExtract one file
tar -xf archive.tar "dir/*"Extract matching path pattern
tar -xf archive.tar --strip-components=1Strip leading directory from paths

List and Inspect

Check what is inside an archive.

CommandDescription
tar -tf archive.tarList files
tar -tvf archive.tarDetailed list with metadata
tar -tf archive.tar | headPreview first entries
tar -tf archive.tar | grep nginxFind matching archive paths
tar --compare -f archive.tarCompare archive with filesystem

Compression Formats

Use gzip, bzip2, and xz with tar.

CommandDescription
tar -czf backup.tar.gz /dataCreate .tar.gz
tar -xzf backup.tar.gzExtract .tar.gz
tar -cjf backup.tar.bz2 /dataCreate .tar.bz2
tar -xjf backup.tar.bz2Extract .tar.bz2
tar -cJf backup.tar.xz /dataCreate .tar.xz
tar -xJf backup.tar.xzExtract .tar.xz

Preserve Metadata

Keep permissions, ownership, and timestamps.

CommandDescription
tar -cpf system.tar /etc /varArchive system paths preserving permissions
tar -xpf rootfs.tarExtract and preserve permissions
tar --numeric-owner -cpf backup.tar /srvStore numeric UID/GID
tar --same-owner -xpf backup.tarKeep original ownership on extract
tar --atime-preserve=system -cf backup.tar /dataPreserve file access times

Incremental Backups

Create snapshot-based incremental archives.

CommandDescription
tar --listed-incremental=snapshot.snar -cf full.tar /homeCreate initial full backup
tar --listed-incremental=snapshot.snar -cf inc-1.tar /homeCreate next incremental backup
tar -tf inc-1.tarList incremental archive entries
tar -xf full.tar -C /restoreRestore full backup
tar -xf inc-1.tar -C /restoreApply incremental backup

Exclude Patterns

Skip files and directories during archive creation.

CommandDescription
tar -cf app.tar app/ --exclude="*.tmp"Exclude by extension
tar -cf app.tar app/ --exclude="cache/*"Exclude path pattern
tar -cf app.tar app/ --exclude-vcsExclude VCS dirs (.git, etc.)
tar -cf app.tar app/ --exclude-from=exclude.txtExclude using a patterns file
tar -czf logs.tar.gz /var/log --exclude="*.gz"Archive logs without already-compressed files

Safety and Verification

Run safer extraction and validation commands.

CommandDescription
tar -tvf archive.tarInspect archive before extracting
tar -xf archive.tar --keep-old-filesDo not overwrite existing files
tar -xf archive.tar --skip-old-filesSkip files that already exist
tar -xf archive.tar --warning=no-unknown-keywordSuppress noisy keyword warnings
tar -tzf backup.tar.gz > /dev/nullQuick integrity check for .tar.gz

Remote Transfer

Pipe tar over SSH for direct server-to-server transfers.

CommandDescription
tar -czf - /data | ssh user@host "cat > backup.tar.gz"Archive and send to remote host
ssh user@host "tar -czf - /data" | tar -xzf - -C /local/pathPull and extract from remote host
tar -cf - /data | ssh user@host "tar -xf - -C /restore"Stream archive directly to remote

Common Options

Useful flags and what they do.

OptionDescription
-cCreate archive
-xExtract archive
-tList archive contents
-fUse archive file
-vVerbose output
-zGzip compression
-jBzip2 compression
-JXZ compression
-CChange to directory before operation
-pPreserve permissions

Use these articles for step-by-step tar workflows.

GuideDescription
How to Create and Extract Archives Using the tar Command in LinuxFull tar tutorial with examples
How to Create tar.gz FileFocused .tar.gz creation guide
How to Extract/Unzip tar.gz File.tar.gz extraction patterns