Creating and extracting archives

3.1 Archiving Files on the Command Line (Weight: 2)

📘Linux Essentials (LPI 010-160)


1. Understanding Archives

An archive file is a single file that contains multiple files and directories.

Important characteristics:

  • It groups files together into one file
  • It preserves directory structure
  • It may store permissions, ownership, and timestamps
  • It can be compressed or uncompressed

Common archive file extensions include:

ExtensionMeaning
.tarStandard archive format
.tar.gzTar archive compressed with gzip
.tgzShort form of .tar.gz
.tar.bz2Tar archive compressed with bzip2
.tar.xzTar archive compressed with xz

Example archive names:

backup.tar
logs.tar.gz
project.tar.bz2

2. The tar Command

The tar command is the main tool used to create and extract archives in Linux.

Originally, tar stood for Tape Archive, because it was designed to store data on backup tapes.

Today it is widely used to:

  • Combine files into archives
  • Extract archives
  • Compress archives
  • Preserve file metadata

General syntax:

tar [options] archive_name files_or_directories

3. Creating Archives

To create an archive, the -c option is used.

Basic Syntax

tar -c -f archive_name.tar files

Options used:

OptionMeaning
-cCreate a new archive
-fSpecify archive filename

Example

tar -cf project.tar project/

This command:

  • Creates an archive named project.tar
  • Includes the project directory and its contents

Example result:

project.tar

This file now contains the entire directory structure.


4. Adding Multiple Files to an Archive

Multiple files and directories can be included when creating an archive.

Example:

tar -cf files.tar file1.txt file2.txt config/

This archive contains:

  • file1.txt
  • file2.txt
  • config/ directory

5. Viewing Archive Contents

Before extracting an archive, it is often useful to view what it contains.

Option used:

-t

Example:

tar -tf project.tar

This displays the list of files stored inside the archive.

Example output:

project/
project/app.conf
project/data/
project/data/log.txt

6. Extracting Archives

To extract files from an archive, the -x option is used.

Basic Syntax

tar -xf archive_name.tar

Options used:

OptionMeaning
-xExtract archive
-fSpecify archive file

Example:

tar -xf project.tar

This command extracts all files and directories stored in project.tar.


7. Extracting to a Specific Directory

Files can be extracted to a specific location using the -C option.

Example:

tar -xf project.tar -C /opt/projects/

This extracts the archive into:

/opt/projects/

This method is commonly used when deploying application packages on servers.


8. Extracting a Specific File

Instead of extracting the entire archive, a specific file can be extracted.

Example:

tar -xf project.tar project/config.yml

Only that file will be extracted.

This is useful when retrieving configuration files from backup archives.


9. Creating Compressed Archives

Archives can be compressed to reduce file size.

Compression is very common in:

  • software distribution
  • server backups
  • log storage
  • transferring files between systems

Common compression tools used with tar:

CompressionOptionExtension
gzip-z.tar.gz
bzip2-j.tar.bz2
xz-J.tar.xz

9.1 Creating a gzip Compressed Archive

Syntax:

tar -czf archive.tar.gz directory/

Options:

OptionMeaning
-cCreate archive
-zCompress with gzip
-fArchive filename

Example:

tar -czf backup.tar.gz /var/log

This creates a compressed archive of log files.


9.2 Extracting gzip Archives

tar -xzf backup.tar.gz

Options:

OptionMeaning
-xExtract
-zgzip compression
-fArchive file

9.3 Creating a bzip2 Archive

tar -cjf archive.tar.bz2 directory/

Option used:

-j

Example:

tar -cjf logs.tar.bz2 /var/log

9.4 Extracting bzip2 Archives

tar -xjf logs.tar.bz2

9.5 Creating an xz Archive

tar -cJf archive.tar.xz directory/

Example:

tar -cJf backup.tar.xz /etc

9.6 Extracting xz Archives

tar -xJf backup.tar.xz

10. Verbose Mode

The -v option shows detailed output while creating or extracting archives.

Example:

tar -cvf project.tar project/

Output may display:

project/
project/app.conf
project/data/
project/data/log.txt

Verbose mode helps administrators verify which files are processed.


11. Common tar Option Combinations

In practice, options are often combined.

Examples:

CommandPurpose
tar -cf archive.tar dir/Create archive
tar -xf archive.tarExtract archive
tar -tf archive.tarList archive contents
tar -czf archive.tar.gz dir/Create gzip archive
tar -xzf archive.tar.gzExtract gzip archive

12. Typical IT Uses of Archiving

Archiving is widely used in system administration.

1. Server Backups

Administrators archive system configuration files:

tar -czf etc-backup.tar.gz /etc

2. Log File Storage

Large log directories are archived before storage.

tar -czf logs-archive.tar.gz /var/log

3. Software Distribution

Software source code is distributed as compressed archives:

software-package.tar.gz

Administrators download and extract them before installation.


4. Application Deployment

Application directories are archived and transferred to another server:

tar -czf app.tar.gz application/

Then extracted on the target server.


13. Important Exam Points

For the Linux Essentials (LPI 010-160) exam, remember the following:

Key Commands

  • tar -cf → create archive
  • tar -xf → extract archive
  • tar -tf → list archive contents

Compression Options

  • -z → gzip
  • -j → bzip2
  • -J → xz

Important Options

OptionPurpose
-cCreate archive
-xExtract archive
-tList contents
-fArchive filename
-vVerbose output
-CExtract to directory

File Extensions to Recognize

  • .tar
  • .tar.gz
  • .tgz
  • .tar.bz2
  • .tar.xz

Summary

Archiving in Linux is the process of combining multiple files and directories into a single archive file. The primary tool used for this purpose is the tar command. Administrators use tar to create archives, extract files, view archive contents, and compress data using gzip, bzip2, or xz. Archiving helps simplify backups, software distribution, and file transfers in IT environments.

Buy Me a Coffee