Tools › Cheat sheets › Linux & Shell › tar
📦

tar cheat sheet

Think of tar's flags as a sentence: create, extract or list, then file (the archive name, which must come right after f). Add z for gzip, j for bzip2, J for xz and v for verbose.

Click any command to copy it.

Create

CommandWhat it does
tar -czf archive.tar.gz dir/Create a gzip-compressed archive of a directory.
tar -cjf archive.tar.bz2 dir/Create a bzip2-compressed archive.
tar -cJf archive.tar.xz dir/Create an xz-compressed archive (smaller, slower).
tar --zstd -cf archive.tar.zst dir/Create a zstd-compressed archive (fast, good ratio).
tar -cf archive.tar dir/Create an uncompressed archive.
tar -czf archive.tar.gz file1 file2 dir/Archive several items.

Extract

CommandWhat it does
tar -xf archive.tar.gzExtract any archive; tar detects the compression.
tar -xzf archive.tar.gz -C /target/dirExtract into a specific directory (it must exist).
tar -xzf archive.tar.gz path/in/archiveExtract a single file or folder.
tar -xzf archive.tar.gz --strip-components=1Remove the top-level folder while extracting.
tar -xpf archive.tar.gzExtract preserving permissions.

Inspect

CommandWhat it does
tar -tf archive.tar.gzList the contents without extracting.
tar -tvf archive.tar.gzList with permissions, owner and sizes.
tar --diff -f archive.tar -C /pathCompare the archive to files on disk.

Excluding and streaming

CommandWhat it does
tar -czf a.tar.gz --exclude='*.log' --exclude='node_modules' dir/Skip patterns when creating.
tar -czf a.tar.gz -T files.txtArchive the files listed in a text file.
tar -czf - dir/ | ssh user@host "tar -xzf - -C /dest"Stream an archive to a remote host without a temporary file.
tar -rf archive.tar newfileAppend a file to an uncompressed archive.
tar -czf backup-$(date +%F).tar.gz dir/Name the archive with today's date.

Frequently asked questions

Why does tar say 'Removing leading /'?

For safety tar strips the leading slash from absolute paths so extraction cannot overwrite system files. Use relative paths or -C to avoid the message.

Which compression should I choose?

gzip (z) is fastest and universal, xz (J) compresses best, and zstd (--zstd) is a good modern balance of speed and size.

More Linux & Shell cheat sheets

🐚 Bash🔎 grep🔍 find✂️ sed🧮 awk📝 Vim🪟 tmux🔐 chmod permissionsAll Linux & Shell cheat sheets →
Copied to clipboard