VMware ESXi and vSphere Cluster Management

Create and Extract TAR Archives on Raspberry Pi

Learn how to create and extract gzip-compressed TAR archives on Raspberry Pi OS using tar, with safe working-directory practices and troubleshooting tips.

What is an archive?

An archive is a single file that packages one or more files and directories together. Combining files into one archive makes them easier to distribute, transfer, store, or back up.

Archiving and compression are different operations. The tar utility collects files and directories into an archive. A compression method such as gzip then reduces the size of that archive.

A tarball is an informal name for a TAR archive. Tarballs are often compressed with gzip or bzip2. Common filename extensions include .tar.gz, .tgz, and .tar.bz2.

Why TAR files matter on Raspberry Pi OS

Linux software, source code, drivers, and utilities are commonly distributed as TAR archives. A downloaded package or source distribution usually must be extracted before you can inspect its files, build it, or install it.

Raspberry Pi OS includes the tar command, so you can work with many software archives directly from the terminal. This lesson assumes that you can open a terminal, navigate between directories, recognize file names, and run commands with arguments.

Basic tar command structure

tar is a Linux utility for creating, reading, and extracting archives. Its name comes from “tape archive,” although it is now commonly used with files stored on disks, SD cards, and network storage.

A typical command follows this pattern:

tar action-options archive-filename source-files-or-directories

The action option tells tar what to do. For example, c creates an archive and x extracts one. Other letters can request verbose output or gzip processing.

The f option associates the command with the archive filename. In commands such as tar cvfz archive.tgz files, the name immediately after the option group is the archive file.

OptionPurposeUsed when

c — create a new archive — creating

x — extract archive contents — extracting

v — display the names of processed files — optional verbose feedback

f — provide the archive filename — creating and extracting

z — use gzip compression or decompression — working with .tgz or gzip-compressed TAR files

Create a gzip-compressed archive

Suppose the current directory contains textfile1.txt and textfile2.txt. Create a gzip-compressed archive named tar-example.tgz with this command:

tar cvfz tar-example.tgz textfile1.txt textfile2.txt

This command packages both text files into one archive and compresses the archive with gzip.

Understanding the creation command

  • tar runs the TAR utility.
  • c creates a new archive.
  • v enables verbose output, so TAR displays the names of files as it processes them.
  • f tells TAR that the next item is the archive filename.
  • z enables gzip compression.
  • tar-example.tgz is the output archive name. The .tgz extension conventionally indicates a gzip-compressed TAR archive.
  • textfile1.txt and textfile2.txt are the input files added to the archive.

The order of the letters in the option group is commonly written as cvfz. On many Linux systems, the hyphen is optional for these short TAR options, so both traditional forms may be encountered. The command shown above uses the conventional Raspberry Pi OS form without a hyphen.

To include a directory and everything inside it, provide the directory name as an input instead of listing individual files. Always check the directory contents first when creating an archive so that you know what will be included.

Extract a gzip-compressed archive

To unpack the previously created archive into the current directory, run:

tar xvfz tar-example.tgz

Here, x changes TAR from archive-creation mode to extraction mode.

  • x extracts the files contained in the archive.
  • v displays the names of files as they are processed.
  • f identifies tar-example.tgz as the archive filename.
  • z tells TAR to decompress gzip-compressed input.

After the command finishes, the restored files normally appear in the shell's current working directory.

TaskCommand patternKey action optionResult

Create compressed archive — tar cvfz archive.tgz filesc — creates a gzip-compressed archive

Extract compressed archive — tar xvfz archive.tgzx — unpacks archive contents

Work safely with extracted contents

By default, TAR extracts files into the current working directory. Before extracting, check where you are and confirm the archive name:

pwd
ls

pwd prints the current directory. ls lists its contents, including the archive if it is stored there.

Extraction can write files with the same names as existing files. Depending on the archive contents and the options used, existing files may be overwritten. To reduce the risk of clutter or accidental replacement, create or choose a dedicated destination directory and navigate there before extraction.

mkdir extracted-files
cd extracted-files
tar xvfz ../tar-example.tgz

This example creates an extracted-files directory, enters it, and extracts the archive from its parent directory. Verify the destination and archive path carefully before pressing Enter.

Common problems and fixes

An input file cannot be found while creating an archive

If TAR reports that an input file cannot be found, the name may be misspelled or the command may be running from a directory that does not contain the file.

<
  • Run pwd to check the current directory.
  • Run ls to inspect the available names.
  • Compare the command with the exact spelling, capitalization, and extension of each input file.

The archive cannot be opened during extraction

This usually means that the archive filename or path is incorrect, or that the command is being run from the wrong directory. Use pwd and ls to confirm that the archive exists, then provide its correct filename or path.

Extracted files appear in an unexpected location

TAR uses the shell's current working directory unless you explicitly select another destination. Navigate to the intended directory before extraction, and use pwd to verify it.

The archive is larger than expected

The archive may have been created without compression, or the selected files may not compress well. For a gzip-compressed TAR archive, include the z option during creation and use an extension such as .tgz or .tar.gz.

Key points

  • An archive packages multiple files or directories into one file.
  • tar performs the archiving; gzip or bzip2 performs compression.
  • Use c to create and x to extract.
  • Use v for visible file-processing feedback and f to specify the archive filename.
  • Use z when creating or extracting gzip-compressed TAR files.
  • Check the current directory and archive path before extraction because files are written there by default.

For a quick reference, return to the TAR archive creation and extraction guide.