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-directoriesThe 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.
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.txtThis command packages both text files into one archive and compresses the archive with gzip.
Understanding the creation command
tarruns the TAR utility.ccreates a new archive.venables verbose output, so TAR displays the names of files as it processes them.ftells TAR that the next item is the archive filename.zenables gzip compression.tar-example.tgzis the output archive name. The.tgzextension conventionally indicates a gzip-compressed TAR archive.textfile1.txtandtextfile2.txtare 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.tgzHere, x changes TAR from archive-creation mode to extraction mode.
xextracts the files contained in the archive.vdisplays the names of files as they are processed.fidentifiestar-example.tgzas the archive filename.ztells TAR to decompress gzip-compressed input.
After the command finishes, the restored files normally appear in the shell's current working directory.
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
lspwd 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.tgzThis 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
pwdto check the current directory. - Run
lsto 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.
tarperforms the archiving; gzip or bzip2 performs compression.- Use
cto create andxto extract. - Use
vfor visible file-processing feedback andfto specify the archive filename. - Use
zwhen 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.