Raspberry Pi online course

Create and Extract tar Archives on Raspberry Pi

Learn how to use tar on Raspberry Pi OS to create, inspect, compress, and safely extract .tar, .tar.gz, .tgz, and .tar.bz2 archives.

The tar command is a standard Linux utility for combining files and directories into an archive. On Raspberry Pi OS, you can use it to package project files, create backups, inspect downloaded software, and transfer groups of files.

This lesson assumes that you can open a terminal and use basic commands such as cd, pwd, and ls. For a refresher, see Terminal In Raspbian and Useful Terminal Commands.

What is an archive?

An archive is a single file containing one or more files or directories. It normally preserves the stored directory structure and useful metadata such as permissions and timestamps.

For example, three separate files can be combined into one archive:

notes.txt
photos/
settings.conf

Archives are useful for:

  • Bundling Linux software source code for distribution.
  • Creating backups of Raspberry Pi projects and configuration files.
  • Transferring many files as one portable file.
  • Sharing a directory while preserving its structure.

Archiving and compression are different

Archiving collects files into one container. The tar command performs this task. Compression reduces the size of data using a method such as gzip or bzip2.

A plain .tar file is an archive without compression. A .tar.gz file is a tar archive compressed with gzip. A .tar.bz2 file is compressed with bzip2.

tar command basics

tar is both a command and an archive format. The name originally referred to tape archives, but tar files are now commonly stored on an SD card, hard disk, or network share.

The general command structure is:

tar [options] archive-name files-or-directories

Options can be written together, as in cvfz or xvfz, or with option prefixes, as in -cvzf and -xvzf. This lesson uses the prefixed style because each option is easier to identify.

Common tar operations and options

OptionPurposeTypical use

c — create an archive — use when bundling files

x — extract an archive — use when unpacking files

t — list archive contents — inspect paths without extracting

v — verbose output — show files as tar processes them

f — supply the archive filename — the archive name follows this option

z — use gzip compression — handle .tar.gz and .tgz

j — use bzip2 compression — handle .tar.bz2

C — change to a directory — choose the extraction destination

The f option is especially important: it tells tar that the next value is the archive filename. For example, in tar -cvf backup.tar files/, backup.tar is the archive name.

Verbose output is optional. It is useful while learning and when troubleshooting because it shows which paths tar is reading or writing.

Check your location before using tar

The current working directory is the directory in which a command runs unless you provide another path. Check it before creating or extracting an archive:

pwd
ls

pwd prints the current directory. ls shows its contents, helping you confirm exact filenames and letter case.

Directory arguments are processed recursively. If you archive my-project/, tar includes the directory and the files and subdirectories beneath it.

Create an uncompressed tar archive

Use the c operation to create an archive and f to name it. This example combines two text files into tar-example.tar:

tar -cvf tar-example.tar textfile1.txt textfile2.txt

Here, c means create, v displays the processed files, and f is followed by the output filename. The remaining arguments are the files to include.

You can include multiple files and a directory in the same command:

tar -cvf project-files.tar README.txt settings.conf my-project/

This creates an uncompressed archive containing both named files and the complete my-project/ directory.

Create gzip-compressed tar archives

gzip is a widely used compression format. Its filename suffix is .gz. A gzip-compressed tar archive is commonly named .tar.gz or .tgz.

Create a compressed archive from two text files with:

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

The options mean:

  • c: create a new archive.
  • v: show verbose output.
  • z: compress the tar data with gzip.
  • f: use the following value, tar-example.tgz, as the archive filename.

A directory and all of its contents can be compressed in the same way:

tar -cvzf project-backup.tar.gz my-project/

The resulting file is convenient for storing or sharing a project backup. Compression usually makes text-heavy collections substantially smaller, although the size reduction depends on the data.

Create bzip2-compressed tar archives

bzip2 is another common compression format. A bzip2-compressed tar archive normally uses the .tar.bz2 extension. Use the j option:

tar -cvjf documents.tar.bz2 documents/

This creates an archive of documents/ and compresses it with bzip2. The equivalent short option group is cvjf.

Recognize common archive filenames

ExtensionArchive and compression typeTypical tar handling

.tar — tar archive with no compression — use options such as -xvf

.tar.gz — tar archive compressed with gzip — use -xvzf

.tgz — alternate name for a gzip-compressed tar archive — use -xvzf

.tar.bz2 — tar archive compressed with bzip2 — use -xvjf

Tarball is an informal name for a tar archive, especially a compressed tar archive. The name of a file is a useful clue, but it can be misleading, so inspect unfamiliar downloads before extracting them.

List archive contents before extraction

Use the t operation to list paths without unpacking anything. Add v for a detailed listing:

tar -tvf tar-example.tgz

A verbose listing can show stored paths, permissions, ownership, sizes, and timestamps where those details are available. Listing first is a good habit for downloaded or unfamiliar archives.

For a bzip2 archive, include j:

tar -tvjf documents.tar.bz2

Extract archives

Extract in the current directory

Use x to extract. A plain tar archive can be unpacked with:

tar -xvf archive.tar

For a gzip-compressed tarball, use z:

tar -xvzf archive.tgz

For a bzip2-compressed archive, use j:

tar -xvjf archive.tar.bz2

Unless you specify another location, extracted files normally appear in the current working directory. Run pwd first if you are not certain where that is.

Extract into a chosen directory

Create a dedicated destination and use the capital -C option:

mkdir -p extracted-files
tar -xvzf tar-example.tgz -C extracted-files

Here, mkdir -p creates the destination if needed, and -C extracted-files tells tar to change to that directory before extracting. This keeps the unpacked files separate from the rest of the current directory.

You can use the same technique with a bzip2 archive:

mkdir -p documents-copy
tar -xvjf documents.tar.bz2 -C documents-copy

Safe path and directory practices

  • Run pwd and ls before choosing archive inputs or an extraction location.
  • Use explicit relative or absolute paths when files are not in the current directory.
  • Archive only the required directories and files; avoid large, unrelated folders.
  • Do not create an archive inside the directory being archived unless you have deliberately excluded it.
  • List downloaded archives before extraction.
  • Extract unfamiliar content into a temporary or empty directory first.
  • Review existing files before extracting into a working project, because matching files may be overwritten.

For broader Raspberry Pi file operations, see File Management and Directory Management.

Working with software source archives

Linux software downloads often arrive as .tar.gz, .tgz, or .tar.bz2 files. Extracting such an archive only unpacks its source code or application files; it does not necessarily install the software.

After extraction, inspect the new directory for documentation such as README or INSTALL. Follow the project's own instructions because building and installing software varies between projects.

Troubleshooting tar commands

Input file cannot be found

If tar reports that an input file does not exist, the name may be misspelled or the terminal may be in another directory.

pwd
ls

Check exact spelling and letter case, then provide the correct relative or absolute path.

tar cannot open the archive

Confirm that the archive exists and that its path is correct:

ls archive.tar.gz

A permissions problem may also prevent access. Check file and directory permissions before considering elevated privileges; using sudo does not fix an incorrect filename.

Files appear in an unexpected location

Extraction uses the current working directory unless -C is supplied. Run pwd before extraction, or create a destination and use:

mkdir -p destination-directory
tar -xvzf archive.tar.gz -C destination-directory

The compression option does not match

An error can occur when a plain tar archive is given the gzip or bzip2 option, or when a filename extension does not describe the actual format. Use z for gzip, j for bzip2, and omit both for a plain .tar file. Inspect the filename and list the archive when possible.

Existing files are overwritten

This can happen when archive paths match files already in the destination. List the archive first, then extract into an empty or temporary directory. Review the result before copying files into an active project.

Extracted software is not installed

An archive may contain source code rather than an installed package. Read its README or INSTALL documentation and follow the project's separate installation steps.

Quick reference

# Check location and files
pwd
ls

# Create an uncompressed archive
tar -cvf archive.tar file1 file2

# Create a gzip-compressed archive
tar -cvzf archive.tar.gz directory/

# Create a bzip2-compressed archive
tar -cvjf archive.tar.bz2 directory/

# List contents without extracting
tar -tvf archive.tar.gz

# Extract a plain tar archive
tar -xvf archive.tar

# Extract a gzip tarball
tar -xvzf archive.tgz

# Extract a bzip2 tarball
tar -xvjf archive.tar.bz2

# Extract to a chosen directory
mkdir -p destination-directory
tar -xvzf archive.tar.gz -C destination-directory