# tar basics: pack, list and unpack a folder

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/tar-basics-full-en
tar packs files into one archive and unpacks them again. `-c` creates, `-x` extracts, `-t` lists, `-z` filters through gzip. `-C` chooses the directory that extraction runs in, so files land under it instead of the current directory.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-25-A, TE-25-B and TE-25-C. The expected non-zero exit is TE-25-C (exit status 2). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### TE-25-A: tar.gz and .tgz are gzip archives; -t lists without extracting; -C extracts into the chosen directory

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
mkdir -p src out; printf 'x\n' > src/a.txt; printf 'y\n' > src/b.txt
```

Command:

```bash
tar -czf pack.tgz src; tar -czf pack.tar.gz src; gzip -t pack.tgz; echo gz1=$?; gzip -t pack.tar.gz; echo gz2=$?; tar -tzf pack.tgz | wc -l; tar -xzf pack.tgz -C out; find out -type f | sort
```

Recorded output:

```
gz1=0
gz2=0
3
out/src/a.txt
out/src/b.txt
```

### TE-25-B: tar -tzf lists the stored paths of a .tgz without extracting it

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
mkdir -p src; printf 'x\n' > src/a.txt; tar -czf pack.tgz src
```

Command:

```bash
tar -tzf pack.tgz | sort; ls
```

Recorded output:

```
src/
src/a.txt
pack.tgz
src
```

Exit status: 0. This status and the output match what was written before the run.

### TE-25-C: expected non-zero exit: extracting an archive that does not exist is an error

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

```bash
tar -xzf nosuch.tgz
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
tar (child): nosuch.tgz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
```

Exit status: 2. This status and the output match what was written before the run.

## Reading the recorded output

The values gz1=0 and gz2=0 are the exit statuses of gzip -t on pack.tgz and on pack.tar.gz, so both files passed the gzip integrity test. The number 3 is the count of lines printed by tar -tzf pack.tgz; by the setup, those are the folder src and its two files. After tar -xzf pack.tgz -C out, find listed out/src/a.txt and out/src/b.txt, so the files were extracted under out.

TE-25-B: by the setup, pack.tgz was made from the folder src. tar -tzf listed src/ and src/a.txt, and the ls after it shows that nothing was extracted: the folder holds only pack.tgz and src. TE-25-C: tar -xzf nosuch.tgz exited with status 2. The first line of standard error is tar (child): nosuch.tgz: Cannot open: No such file or directory.

## Limits

This page covers create, list, extract and the gzip filter with relative paths. Absolute paths, ownership, sparse files and compression levels are not tested here.

## Source

- https://www.gnu.org/software/tar/manual/

## Evidence

- `TE-25-A`: tar.gz and .tgz are gzip archives; -t lists without extracting; -C extracts into the chosen directory

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-25-A` | tar.gz and .tgz are gzip archives; -t lists without extracting; -C extracts into the chosen directory | 0 | PASS | `1038cb04247c` |
| `TE-25-B` | tar -tzf lists the stored paths of a .tgz without extracting it | 0 | PASS | `26ac0908dfe3` |
| `TE-25-C` | expected non-zero exit: extracting an archive that does not exist is an error | 2 | PASS | `5d4613d1b2ba` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [curl basics: fetch a page, save it, read the status code](https://kairos-2z6s.onrender.com/res/curl-basics-full-en)
- [sed basics: replace text in a stream](https://kairos-2z6s.onrender.com/res/sed-basics-full-en)
- [Answer questions about a log file with a pipeline](https://kairos-2z6s.onrender.com/res/log-pipeline-full-en)
- [grep: search text, with the options worth knowing](https://kairos-2z6s.onrender.com/res/grep-basics-full-en)
- [Reading a failed Python test, line by line](https://kairos-2z6s.onrender.com/res/unittest-failure-full-en)
- [sort, uniq and cut: count things in a text file](https://kairos-2z6s.onrender.com/res/sort-uniq-cut-full-en)
