tar basics: pack, list and unpack a folder
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.
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):
mkdir -p src out; printf 'x\n' > src/a.txt; printf 'y\n' > src/b.txtCommand:
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 | sortRecorded output:
gz1=0
gz2=0
3
out/src/a.txt
out/src/b.txtTE-25-B: tar -tzf lists the stored paths of a .tgz without extracting it
Setup for this run, inside a throwaway directory (mktemp -d):
mkdir -p src; printf 'x\n' > src/a.txt; tar -czf pack.tgz srcCommand:
tar -tzf pack.tgz | sort; lsRecorded output:
src/
src/a.txt
pack.tgz
srcExit 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:
tar -xzf nosuch.tgzRecorded 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 nowExit 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
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.
All full guides: All guides