Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Letters to Know

LetterMeaning
cCreate an archive
xExtract an archive
tList the contents
fArchive file name follows
vShow each file
zCompress with gzip
JCompress with xz
aPick compression by suffix

Every command has exactly one of c, x, or t, and almost always f. Because f takes the next word as the archive name, keep it last in the group: -czf site.tar.gz works, -cfz site.tar.gz does not. tar is a GNU project on every current distribution, Ubuntu 26.04 included.

Create an Archive

tar -czf site.tar.gz /var/www/example.com
tar -czvf configs.tar.gz nginx/ php/ notes.txt
tar -caf data.tar.xz data/

With an absolute path, tar removes the leading slash from the names it stores and prints a short note about it. That is a safety feature: the archive later extracts into the current folder instead of over the original location. To store clean relative names without the note, change into the parent folder with -C:

tar -czf site.tar.gz -C /var/www example.com

List the Contents

tar -tf site.tar.gz
tar -tvf site.tar.gz | less

Check what is inside before you extract an archive someone else made. It may hold many files at the top level instead of one folder, or paths you do not expect. When reading an archive, tar detects gzip, xz, and the other formats by itself, so -tf and -xf work without the compression letter.

Extract

tar -xf site.tar.gz
tar -xf site.tar.gz -C /srv/restore
tar -xf site.tar.gz example.com/public/index.php

The -C folder must already exist. Extraction overwrites files of the same name without asking, so extract into an empty folder first when you are not sure. The full set of extraction cases is in how to extract tar.gz files.

Exclude Files

tar -czf site.tar.gz --exclude="*.log" --exclude="node_modules" -C /var/www example.com
tar -czf home.tar.gz --exclude-vcs -C /home deploy

Put the exclude options before the folder names; current tar versions warn when they come after. Quote the patterns so the shell does not expand them. --exclude-vcs skips version control folders such as .git, and --exclude-from=FILE reads one pattern per line from a file.

Permissions and Ownership

tar stores the owner, group, and mode of every file. When root extracts, tar restores the original owners and permissions; when a normal user extracts, that user becomes the owner and the user's umask applies, unless you add -p to keep the stored modes. Owners are restored by name when that user exists on the target server. For access control lists and extended attributes, add the options on both sides:

sudo tar --acls --xattrs -czf etc.tar.gz -C / etc
sudo tar --acls --xattrs -xzf etc.tar.gz -C /srv/restore

gzip, xz, or zstd

OptionSuffixTrade-off
-z.tar.gzFast, works everywhere
-j.tar.bz2Older, slow
-J.tar.xzSmallest, slowest
--zstd.tar.zstFast and small

zstd needs the zstd program: sudo apt install zstd on Debian and Ubuntu, sudo dnf install zstd on AlmaLinux and Rocky Linux. For a quick backup of a large folder it is usually the best balance.

Copy a Folder over SSH with tar

tar -czf - -C /var/www example.com | ssh user@your_server_ip "tar -xzf - -C /var/www"
ssh user@your_server_ip "tar -czf - -C /var/www example.com" > example.com.tar.gz

A dash in place of the file name means "write to the pipe" or "read from the pipe". No temporary archive is written, which helps when the source disk is almost full. For copies you repeat, the rsync command is better, because it sends only what changed. If the other side expects a .zip file, see how to zip files in Linux.

Was this answer helpful?
Loading...