Commands.page Logo

How to Create Tar Archive Using Standard Output in Ubuntu

This article provides a concise explanation of how to generate a tar archive directly to standard output on Ubuntu. It covers the specific command syntax needed to stream archive data without saving a temporary file, enabling efficient piping to other tools or network connections.

To create a tar archive and send it to standard output instead of a file, you use the hyphen character - as the filename argument. The basic syntax requires the create flag -c and the file flag -f, followed by the dash.

tar -cf - [directory_or_files]

Replace [directory_or_files] with the path to the content you wish to archive. The dash tells the tar utility to write the resulting archive data to standard output.

Example Usage

To archive a folder named data and send it to stdout:

tar -cf - data

Common Use Cases

This syntax is primarily used for piping. You can compress the stream immediately using gzip:

tar -cf - data | gzip > archive.tar.gz

You can also transfer the archive directly over SSH without creating a local file:

tar -cf - data | ssh user@host "tar -xf -"

This method saves disk I/O and keeps your workspace clean by avoiding intermediate archive files.