Extract archive

2 snippets across 2 stacks - Bash & Linux, Docker

SHBash & Linux

Extract a Tar Archive

SH · Archives & Compression
Syntax
tar -xf archive.tar [-C directory]
Example
tar -xzf backup_20260404.tar.gz -C /opt/restore/
tar -xf release.tar.bz2
tar -tf archive.tar.gz

Note -x extracts, -C specifies the target directory. tar auto-detects compression on modern systems, so -xf often works without specifying -z/-j/-J. -t lists contents without extracting (always do this first on untrusted archives). WARNING: Tar archives can contain absolute paths or ../ that overwrite files outside the target; use -t to inspect first.

DKDocker

ADD - Copy with Extras

DK · Dockerfile
Syntax
ADD <src> <dest>
Example
ADD app.tar.gz /app/
ADD https://example.com/config.json /app/config.json

Note ADD can auto-extract tar archives and fetch URLs. However, COPY is preferred for simple file copies because ADD's extra behavior can be surprising. Using ADD for URL downloads also defeats layer caching - use RUN curl instead for better control.

Frequently asked questions

How do you extract archive?
This task is covered in 2 stacks on this page: Bash & Linux, Docker. The "Extract a Tar Archive" snippet in Bash & Linux uses `tar -xf archive.tar [-C directory]`.
Which command does the Bash & Linux example use?
The "Extract a Tar Archive" snippet uses `tar -xf archive.tar [-C directory]`, from the Archives & Compression section of the Bash & Linux cheat sheet.
Which stacks cover "extract archive" on this page?
Bash & Linux, Docker. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Extract a Tar Archive": -x extracts, -C specifies the target directory. tar auto-detects compression on modern systems, so -xf often works without specifying -z/-j/-J. -t lists contents without extracting (always do this first on untrusted archives). WARNING: Tar archives can contain absolute paths or ../ that overwrite files outside the target; use -t to inspect first.