Bash 5 reference · Text and System Tools

tar (Linux command)

Create, list and extract archives, usually gzip-compressed .tar.gz files, preserving directory structure and permissions.

Bash 5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

tar -czf ARCHIVE.tar.gz PATHS   tar -tzf ARCHIVE   tar -xzf ARCHIVE [-C DIR]

Parameters

ParameterDescription
-c / -x / -t Create / extract / list.
-z / -J gzip / xz compression.
-f FILE Archive file.
-C DIR Change to DIR (extract into it).
--exclude PATTERN Skip matching files.

Examples

Create with an exclusion
mkdir -p app/logs; echo hi > app/main.sh; echo x > app/logs/a.log
tar -czf app.tgz --exclude='*.log' app
tar -tzf app.tgz | sort

Output

app/
app/logs/
app/main.sh

Run this on your computer — see “Get Started” for setup.

Extract into a directory
mkdir src; echo data > src/f; tar -czf s.tgz src
mkdir out; tar -xzf s.tgz -C out; cat out/src/f

Output

data

Run this on your computer — see “Get Started” for setup.

Was this page helpful?