TExcelTAD
Home › Linux & Bash › tar: Archives

tar: Archives

tar (tape archive, from the days of backup tapes) packs many files and directories into one archive file, keeping names, folders, permissions and timestamps. Combined with compression (gzip, xz, bzip2) it is the standard way to back up, ship and download software on Linux: files like project.tar.gz or node-v24.tar.xz are everywhere.

The three main operations

Operation Short flag Meaning
Create c Make a new archive
List t Show what's inside
Extract x Unpack the files

They're combined with f FILE (the archive file name), v (verbose) and a compression flag. Read tar -czvf as create, gzip, verbose, file.

Create and list

bash · try it yourself
mkdir -p website/css
echo "<h1>Hi</h1>" > website/index.html
echo "body{}" > website/css/site.css
tar -cf website.tar website
tar -tf website.tar | sort
website/
website/css/
website/css/site.css
website/index.html

Paths in the archive are stored relative to where you ran tar, so the archive contains website/... and will recreate that folder when extracted. (The members are stored in the order the filesystem returns them, which can differ between machines; the examples sort the listing so it is always the same.)

Compression

Flag Compressor Extension
-z gzip .tar.gz or .tgz
-j bzip2 .tar.bz2
-J xz .tar.xz
-a choose from the file name any of the above
bash · try it yourself
mkdir logs
for day in 01 02 03; do
  for i in $(seq 1 200); do echo "2026-03-$day request $i OK"; done > logs/access-$day.log
done
tar -czf logs.tar.gz logs
tar -cJf logs.tar.xz logs
file logs.tar.gz logs.tar.xz | cut -d, -f1
cat logs/*.log | wc -c
[ "$(stat -c %s logs.tar.gz)" -lt 2000 ] && echo "the .tar.gz is under 2 KB"
logs.tar.gz: gzip compressed data
logs.tar.xz: XZ compressed data
15276
the .tar.gz is under 2 KB

Repetitive text like logs compresses extremely well: 600 lines of log shrink to a tiny fraction. file identifies an archive's real type regardless of its extension.

Extracting

-x extracts into the current directory; -C DIR extracts somewhere else. tar detects the compression automatically when reading, so tar -xf works for .tar.gz and .tar.xz alike:

bash · try it yourself
mkdir -p src && echo "print('v1')" > src/app.py && echo "# App" > src/README.md
tar -czf release.tar.gz src
rm -r src
mkdir restore
tar -xzf release.tar.gz -C restore
find restore | sort
echo "---"
tar -xzf release.tar.gz -C restore src/app.py --to-stdout
restore
restore/src
restore/src/README.md
restore/src/app.py
---
print('v1')

You can extract just some files by naming them after the archive, and -O / --to-stdout prints a file's content instead of writing it.

Warning: Extracting overwrites existing files with the same names, silently. List an unknown archive with tar -tf first: some archives aren't wrapped in a single top-level folder and will scatter files across your current directory (a "tarbomb").

Verbose listings

-tv shows details like ls -l. The times are the files' modification times stored in the archive:

bash · try it yourself
mkdir -p docs
echo "draft" > docs/plan.txt
touch -d '2026-02-14 12:00' docs/plan.txt docs
tar -cf docs.tar docs
tar -tvf docs.tar
drwxr-sr-x learner/learner   0 2026-02-14 12:00 docs/
-rw-r--r-- learner/learner   6 2026-02-14 12:00 docs/plan.txt

Excluding files and a dated backup

bash · try it yourself
mkdir -p app/{src,node_modules/pkg,.git}
touch app/src/index.js app/node_modules/pkg/big.js app/.git/HEAD app/debug.log
day="2026-03-15"
tar -czf "backup-$day.tar.gz" --exclude=node_modules --exclude='.git' --exclude='*.log' app
tar -tzf "backup-$day.tar.gz" | sort
ls *.tar.gz
app/
app/src/
app/src/index.js
backup-2026-03-15.tar.gz

In a real backup script you'd use day=$(date +%F) to stamp the archive with today's date (it's fixed here so the output never changes).

Cheat sheet

Task Command
Create .tar.gz tar -czf out.tar.gz dir/
List contents tar -tf file.tar.gz
Extract here tar -xf file.tar.gz
Extract into dir tar -xf file.tar.gz -C target/
Extract one file tar -xf file.tar.gz path/in/archive
Add to an uncompressed tar tar -rf file.tar newfile

Test yourself

Which command creates a gzip-compressed archive site.tar.gz from the directory site?

  1. tar -xzf site.tar.gz site
  2. tar -czf site.tar.gz site
  3. tar -tzf site site.tar.gz
  4. gzip site

How do you see what an archive contains without extracting it?

  1. tar -xf file.tar
  2. tar -tf file.tar
  3. cat file.tar
  4. tar -cf file.tar

Which option extracts into a different directory?

  1. -d DIR
  2. -o DIR
  3. -C DIR
  4. --into DIR