The txtar command provides a command-line interface for working with text-based file archives in the txtar format. It enables you to create, extract, and list files stored in text archives, making it useful for bundling files, test data, and documentation examples.
go get golang.org/x/exp/cmd/txtar or go install golang.org/x/exp/cmd/txtar@latestThe txtar command manipulates text-based file archives in a human-readable format. These archives combine file metadata and content in a single text file, making them ideal for:
The txtar format stores files as plain text with clear delimiters, allowing archives to be edited in any text editor and easily understood by humans and programs alike.
The txtar command operates in three modes:
Archives include a comment section at the beginning (separated from files by a blank line), which you can use to document the archive contents or provide metadata.
txtar *.go <README >testdata/example.txtThis reads a comment from stdin (terminated by EOF or a blank line), then packages all .go files and the README file into an archive, writing it to testdata/example.txt.
txtar --extract <playground_example.txt >main.goThis reads the archive from playground_example.txt, extracts all files to their archived locations relative to the current directory, and writes the archive's comment to stdout.
txtar --list <archive.txtThis displays all files contained in the archive without extracting them or expanding shell variables.
Creates text-based archives from files and directories on disk.
Command:
txtar [files/directories...] <stdin >archive.txtBehavior:
.)Environment Variables:
Extracts files from a text-based archive to disk locations.
Command:
txtar --extract [--unsafe] <archive.txtOr using short form:
txtar -x [--unsafe] <archive.txtBehavior:
os.Expand if the corresponding environment variable is setFlags:
--unsafe: Allows extraction to locations outside the current directory and its subdirectories (use with caution)Lists all files contained in an archive without extracting or modifying them.
Command:
txtar --list <archive.txtBehavior:
The txtar tool supports shell variable expansion during extraction, allowing you to create dynamic archives:
Archiving with variables:
# Variables in paths are preserved as-is
txtar $HOME/project/file.go >archive.txt
# Archive will contain: $HOME/project/file.goExtracting with variables:
# Set environment variables before extraction
export OUTPUT_DIR=/tmp/output
txtar --extract <archive.txt
# If archive contains $OUTPUT_DIR/file.txt, it will be extracted to /tmp/output/file.txtBy default, txtar prevents extraction of files outside the current directory tree:
# This fails by default (path tries to escape current dir)
# Error: file path attempts to escape current directory
# Use --unsafe to allow extraction anywhere (use carefully)
txtar --unsafe --extract <archive.txtThe command automatically includes directory contents recursively:
# Include all files in src/ and docs/ directories plus specific files
txtar src/ docs/ main.go config.yaml <description.txt >archive.txtThis archives:
src/ directory and subdirectoriesdocs/ directory and subdirectoriesmain.go fileconfig.yaml filePlus the comment from the provided description.
Create complex workflows using pipes:
# Create archive with inline comment
echo "Test data for example" | txtar *.go >tests.txt
# Extract and process files
txtar --extract <tests.txt | head -20
# Chain operations
cat original.txt | txtar --extract | tar czf output.tar.gz
# List files and filter
txtar --list <archive.txt | grep -E "\.go$"The txtar format is a human-readable text-based archive format consisting of:
-- file_path line indicating the filename-- line or end of fileExample archive structure:
This is the archive comment.
It can span multiple lines.
It describes what's in the archive.
-- main.go
package main
func main() {
fmt.Println("Hello")
}
-- config.yaml
server: localhost
port: 8080
-- nested/readme.txt
This file is in a nested directory.# Create an archive with test input
cat <<'EOF' | txtar *.go >testdata/fixture.txt
Test suite data for TestProcessing
EOF# Package example code with its expected output
echo "Example: Processing configuration files" | \
txtar example.go example.yaml >docs/example.txt# Archive an entire project for later restoration
echo "Project backup - $(date)" | \
txtar src/ tests/ config/ build.sh >backups/project.txt# Create test input from multiple source files
echo "Integration test input" | \
txtar input1.json input2.json input3.json >test_data.txtThe txtar format is widely used in Go's standard testing patterns, particularly for:
Since txtar archives are plain text, they integrate naturally with Git and other version control systems:
The txtar command is a standalone executable and does not export type definitions. It operates through command-line flags and standard I/O streams.
Standard I/O:
Command-line Arguments:
--extract / -x, --list, --unsafe