Skip to content
Latchkey

tar -c: Create an Archive (Usage & CI Errors)

tar -c packs files and directories into one archive stream or file.

Creating archives is the first half of every cache and artifact step. The flags decide what goes in and where the archive lands.

What it does

tar -c (create) reads the named files and directories and writes them into a single archive. On its own tar writes to standard output; pair it with -f to name an output file. Directories are added recursively by default.

Common usage

Terminal
tar -cf archive.tar dir/
tar -cvf archive.tar file1 file2 dir/      # verbose: list members
tar -czf archive.tar.gz dir/               # gzip-compressed
tar -cf - dir/ | ssh host 'cat > backup.tar'   # to stdout, piped

Options

FlagWhat it does
-c / --createCreate a new archive
-f <file>Write to <file> instead of stdout (use - for stdout)
-v / --verboseList each file as it is processed
-z / --gzipFilter the archive through gzip
-C <dir>Change to <dir> before adding the following paths

In CI

Packaging build output as an artifact is a tar -c step: tar -czf dist.tar.gz -C build . creates a gzip archive of the build/ contents without the leading build/ prefix. Upload that single file, then extract it on the consumer job.

Common errors in CI

tar: dir: Cannot stat: No such file or directory followed by tar: Exiting with failure status due to previous errors means a path you listed does not exist relative to the current directory. tar still creates the archive from the parts it could read but exits non-zero, failing the job. Check the working directory and use -C to anchor paths.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →