Each page covers synopsis, options, supported formats or extensions, safety and placement rules (x) or compression profiles and refusals (c), the progress display, exit statuses, environment and examples. c.1 renders its profile table with tbl. make install now installs them to $(PREFIX)/share/man/man1 (MANDIR is overridable), and make uninstall removes them.
archive-utils
Two small command-line tools for Linux:
xextracts archives and compressed files.ccreates archives.
They implement no archive format themselves. They give one consistent,
predictable and safe interface to established tools: bsdtar, GNU tar,
7-Zip, Info-ZIP zip, and the standard compressors. Both are written in Go
and build as single static binaries.
x archive.tar.zst # -> ./archive/ or its single top-level entry
x -d /tmp/extracted a.zip b.7z # several archives into one directory
c backup.tar.zst ./project # format from the extension
c -b backup.tar.xz ./project # smallest output
c -t zip release.zip ./dist
Design
- Nothing is overwritten or deleted by default. Source archives are never
removed. Existing files are replaced only with
--force. - All or nothing. Both tools write into a private temporary directory
beside the destination. The result is moved into place with an atomic,
no-replace rename (
renameat2(RENAME_NOREPLACE)), and only after the backend has succeeded. A failed, corrupt, unsafe or interrupted run leaves nothing behind. - Validate first. Every input, the destination and the required backends are checked before any work starts, and all problems are reported together.
- One backend per format. There are no fallback chains. Backends run as argument vectors, never through a shell.
- Quiet by default. Status and errors go to stderr, and stdout stays empty
(except for
--helpand--version). Color is used only when stderr is a terminal, and never whenNO_COLORis set orTERM=dumb. - Live progress on a terminal, in the style of zap, described below.
DESIGN.md explains each decision.
Supported formats
| Format | Extensions | x backend |
c backend |
|---|---|---|---|
| tar | .tar |
bsdtar | GNU tar |
| tar + gzip | .tar.gz .tgz |
bsdtar | GNU tar + gzip |
| tar + bzip2 | .tar.bz2 .tbz2 .tbz |
bsdtar | GNU tar + bzip2 |
| tar + xz | .tar.xz .txz |
bsdtar | GNU tar + xz |
| tar + zstd | .tar.zst .tzst |
bsdtar | GNU tar + zstd |
| tar + lzma | .tar.lzma |
bsdtar | — |
| gzip, compress | .gz .Z |
gzip | — |
| bzip2 | .bz2 |
bzip2 | — |
| xz, lzma | .xz .lzma |
xz | — |
| zstd | .zst |
zstd | — |
| ZIP | .zip .jar .war .ear |
bsdtar | zip (.zip) |
| 7z | .7z |
7-Zip | 7-Zip |
| RAR (v4, v5) | .rar |
7-Zip | — |
| Cabinet | .cab |
bsdtar | — |
| ISO 9660 (+Rock Ridge/Joliet) | .iso |
bsdtar | — |
| ISO with UDF | .iso |
7-Zip | — |
| cpio | .cpio |
bsdtar | — |
| Debian package | .deb |
bsdtar | — |
| RPM package | .rpm |
bsdtar | — |
Extensions are matched case-insensitively. For .iso, x reads the image's
volume recognition sequence. Images that contain a UDF filesystem (for
example, Windows installation media) go to 7-Zip, and all others go to bsdtar.
Dependencies
At run time, the binaries need nothing except the backends below. Each backend is checked only when a format needs it. A missing one is named in the error, and the tool exits with status 4.
| Arch package | Needed for |
|---|---|
libarchive (bsdtar) |
x: every tar variant, ZIP family, cab, ISO 9660, cpio, deb, rpm |
tar (GNU tar) |
c: every tar variant |
gzip |
x: .gz, .Z · c: tar.gz |
bzip2 |
x: .bz2 · c: tar.bz2 |
xz |
x: .xz, .lzma · c: tar.xz |
zstd |
x: .zst · c: tar.zst |
7zip (7-Zip ≥ 25) |
x: .7z, .rar, UDF ISO · c: 7z |
zip |
c: zip |
sudo pacman -S --needed libarchive tar gzip bzip2 xz zstd 7zip zip
unrar, unzip, cabextract, cpio and rpm2cpio are not needed.
To build: go (1.26 or newer). The only module dependency is
golang.org/x/sys, which provides renameat2 and terminal detection.
To test: the backends above, plus unzip and binutils (ar), which
check c's output independently. zsh and cdrtools (genisoimage, for
the UDF test) are optional. A test whose tools are missing is skipped, not
failed. RPM, CAB, RAR, .Z and malicious archives are generated by the test
code itself.
Installation
make check # gofmt, go vet, staticcheck (if installed), all tests
make install # static binaries to ~/.local/bin, man pages to ~/.local/share/man
make uninstall
Use make install PREFIX=/usr/local (as root) for a system-wide install.
Arch Linux package
An AUR package, archive-utils, is maintained separately from this
repository. It builds from the release tags and follows the Arch Go package
guidelines: PIE binaries, makepkg's flags, -trimpath, and the tests in
check(). It installs /usr/bin/x, /usr/bin/c and the docs, depends on
libarchive, and lists the other backends as optional dependencies.
Oh My Zsh users: the
extractplugin definesalias x=extract, and an alias takes precedence over~/.local/bin/xin interactive zsh. Removeextractfromplugins=(…), or rununalias xafter OMZ loads. Check whichxis active withwhence -v x.
x: extract
x [options] <archive> [archive ...]
-d, --dest DIR extract into DIR (created if missing; default: .)
-f, --force overwrite existing files and merge into existing directories
-k, --keep keep source archives (always the case; accepted for habit)
-q, --quiet print errors only
-v, --verbose list extracted entries
-h, --help show help
--version show version
Where results go. One rule applies to every format:
| Archive contains | Result |
|---|---|
one top-level entry (proj/… or file.txt) |
DEST/proj/ or DEST/file.txt |
| several top-level entries | DEST/<archive name without extension>/ |
| nothing | nothing is created (warning, exit 0) |
a standalone compressed file name.gz |
DEST/name |
As a result, an archive never spills loose files into a directory, and you
never get proj/proj/. Packages follow the same rule. For a .deb, only the
payload (data.tar.*) is extracted, as dpkg-deb -x does. For an .rpm, the
cpio payload is extracted. Package metadata and scripts are not.
Existing paths. If the result already exists, x fails and changes
nothing. With --force:
- an existing file is replaced;
- an existing directory is merged into: files from the archive replace files at the same path, and nothing else in the directory is touched;
- a file never replaces a directory, and a directory never replaces a file;
- symlinks in the destination are never followed;
- the archive being extracted is never overwritten.
Options may appear after operands, and -- ends options. Short options can be
combined (-qf), and values can be attached (-dDIR, --dest=DIR).
c: create
c [options] <output> <input> [input ...]
-t, --type TYPE tar, tar.gz, tar.bz2, tar.xz, tar.zst, zip, 7z
-f, --fast favour speed over size
-b, --best favour size over speed
-q, --quiet print errors only
-v, --verbose list archived entries
--force replace an existing output file
--exclude NAME skip files/directories whose name matches the glob NAME
-h, --help show help
--version show version
Type. The type comes from the output's extension (see the table above) or
from --type. An unknown extension without --type is an error: c never
guesses a format or renames the output. If --type contradicts a known
extension, that is also an error. With an unknown extension, --type is used
and the name is kept as given.
What gets stored. Each input is stored under its own name, wherever it is located:
c out.tar.gz ../src /etc/hosts . # stores src/…, hosts, <current dir name>/…
Inputs are resolved like realpath. A symlink given as an input is
followed, and the target is stored under the target's name. Symlinks inside
directories are stored as symlinks in every format. Two inputs with the same
name are an error, because they would collide inside the archive.
Profiles. "Best" is the highest standard level. Memory-hungry modes
(zstd --ultra, xz -e) are never used.
| Type | --fast |
default | --best |
|---|---|---|---|
| tar.gz | gzip -1 |
-6 |
-9 |
| tar.bz2 | bzip2 -1 |
-9 |
-9 |
| tar.xz | xz -T0 -1 |
-6 |
-9 |
| tar.zst | zstd -T0 -1 |
-3 |
-19 |
| zip | zip -1 |
-6 |
-9 |
| 7z | -mx=1 |
-mx=5 |
-mx=9 |
| tar | (no effect; prints a warning) |
If both options are given, the last one wins.
Exclusion. --exclude NAME skips every file or directory whose name
matches the shell glob NAME, at any depth (--exclude node_modules --exclude '*.o'). It behaves the same for tar, zip and 7z. Patterns that
contain / are rejected.
Refused outright:
- an existing output (without
--force); - an output that is a directory;
- an output inside one of the inputs, which would include itself;
- an output that is also an input;
- a missing output directory;
/as an input.
On success, c reports the archive size: c: created 'backup.tar.zst' (12.4 MiB).
Progress display
On a terminal, both tools show a live display while the backend works:
extract ⠏ big.tar.bz2
████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░
data 24.1 MiB / 40.3 MiB · 25.1 MiB/s · eta <1s
The look follows zap: Catppuccin Mocha colors, a bar that blends from blue
(#89b4fa) to mauve (#cba6f7) over a #313244 track, a braille spinner
refreshed every 80 ms, and a smoothed speed with an ETA. When the operation
finishes, the display is erased and the normal one-line result is printed,
so the terminal looks as if the display had never been there.
- What is measured: the bytes the backend process has read (
rcharin/proc/PID/io). Forx, this is compared with the archive's size. Forc, it is compared with the total size of the inputs after--exclude. For tar formats,cwatches the compressor, which is the slow stage. - "finishing…": shown once everything has been read but the backend is
still working, either writing the last files or compressing buffered input.
Multithreaded
xzandzstdread input ahead in large blocks, so with--beston small inputs the display goes straight to this state. - When it appears: only when stderr is a terminal at least 40 columns
wide, only at normal verbosity (
-qand-vturn it off), and only after 250 ms, so quick operations don't flicker. WithNO_COLOR, the same display is drawn without color. - Backend messages (warnings and errors) are printed above the display, never mixed into it.
- Password prompts: if a backend asks for a password, the display stops so it doesn't overwrite the prompt.
Exit status (both tools)
| Code | Meaning |
|---|---|
| 0 | success |
| 1 | an operation failed (backend error, corrupt or unsafe archive) or the target already exists |
| 2 | usage error |
| 3 | bad input: missing, unreadable or unsupported file; invalid destination or output path |
| 4 | a required program is not installed |
| 128+n | interrupted by signal n (130 for Ctrl-C), after cleaning up |
x validates everything first and exits with 2, 3 or 4 before extracting
anything. If one archive then fails, the rest are still processed, and the
exit status is 1.
Safety
- Path traversal and absolute paths. bsdtar refuses entries containing
.., including hard-link targets, and strips a leading/. For 7z, RAR and UDF, 7-Zip rewrites..and/so that entries stay inside the output directory. In both cases, entries are written inside a fresh staging directory, never directly into the destination. - Symlink attacks. bsdtar refuses to write through a symlink that the
archive itself created. 7-Zip (version 25 or newer) refuses dangerous
links. Because staging starts empty, symlinks that already exist in the
destination are never followed, including during a
--forcemerge. - Unsafe or corrupt archives make the backend fail.
xreports the failure and discards the staging directory, so nothing is written. The test suite checks this with crafted../, absolute, symlink-escape and hard-link-escape archives. - Ownership recorded in archives is ignored. Files belong to the user who
runs
x. - Interrupts (Ctrl-C, SIGTERM, SIGHUP) stop the backend immediately and remove the temporary directory. A second signal kills the tool at once.
Known limitations
- The format is chosen by extension, not by content. If the content doesn't match, the backend fails and nothing is written, but no other format is tried.
- Encrypted archives: bsdtar and 7-Zip prompt for a password on the terminal. There is no password option.
- Not supported:
- multi-volume archives (
.7z.001,.part1.rar); .tar.lz,.lz4,.lzo,.zpaq,.apk,.whland.msi;- standalone compressed files from
c(usegziporzstddirectly); - writing archives to stdout from
c.
- multi-volume archives (
- Staging needs space on the destination filesystem for the full extracted size, which is the space the result needs anyway.
--forcemerges are not atomic. A file-versus-directory conflict deep in a tree stops the merge with an error, and entries already moved stay moved. Merging into read-only directories (such as ones extracted from ISO images) fails.- ISO: UDF images are read through 7-Zip, which ignores Unix permissions and symlinks. Rock Ridge ISO 9660 images keep their usually read-only modes.
- RAR depends on the RAR codec in Arch's
7zippackage. crequires GNU tar for tar formats (it uses--add-file) and Info-ZIP for zip. If a file changes while it is being archived, GNU tar fails, andcreports that failure.- If
--excluderemoves everything, zip fails ("nothing to archive"), while tar and 7z produce an empty archive. -fmeans--forceinxand--fastinc, as specified.ctherefore accepts force only as--force, so a stray-fcan never overwrite anything.- Linux only: the tools use
renameat2(2)and Linux terminal ioctls. - The tools are not designed to run as root. Ownership is never restored, and device nodes in archives fail to extract.
Project layout
cmd/x, cmd/c entry points (one line each)
man/ x(1) and c(1) manual pages
internal/cli GNU-style option parser, logger, exit codes, signal-aware main
internal/proc runs backend pipelines (pipefail semantics; SIGTERM on cancel)
internal/fsx no-replace rename, read-only-safe move, merge and removal
internal/progress zap-style live display: renderer and backend monitor
internal/extract x: format detection, UDF probe, backends, staging and placement
internal/create c: types, profiles, validation, backends
tests/ end-to-end tests: build both binaries, run real archives
Testing
make check # lint + all tests
go test ./... # all tests
go test ./internal/... # unit tests only (no backends needed)
go test ./tests -run 'TestX.*Unsafe' -v
go test -short ./... # skip the slower terminal tests
Unit tests cover option parsing, format detection, UDF probing, the exact argument vector of every backend and profile, pipelines, and the filesystem primitives:
- no-replace rename;
- moving read-only directories;
- merge semantics, including refusing to follow destination symlinks;
- removing read-only trees without following symlinks.
End-to-end tests build x and c and run them in temporary directories.
Every format makes a round trip against a tree with awkward names (spaces,
quotes, Unicode, leading dashes, dotfiles), empty directories and symlinks.
c's output is also checked with each format's own tools (gzip -t,
unzip -t, 7z t, …), independently of x. The progress display is tested
in two ways. Unit tests drive it with a fake terminal and clock: layout,
gradient, width limits, smoothing, erasing, interleaved messages and prompts.
End-to-end tests run x on a real pseudo-terminal through script(1). No
real user files are touched.
License
Public domain (Unlicense).