Before diving into package managers, it's essential to understand file archiving and compression. When you download software online, you'll often find it packaged in archived and compressed formats. This lesson focuses on two fundamental utilities for this purpose: tar and gzip.
Understanding Archiving vs. Compression
It's important to distinguish between archiving and compression. Archiving is the process of combining multiple files and directories into a single file, known as an archive. This makes it easier to manage and transfer a group of files. Compression, on the other hand, is the process of reducing the size of a file to save disk space and speed up transfers. The tar utility is used for archiving, while gzip is used for compression. Often, you will see gzip and tar used together.
Compressing Single Files with gzip
The gzip program is used to compress individual files in Linux. When you compress a file with gzip, it is replaced by a file with a .gz extension.
To compress a file:
gzip mycoolfile
This will create mycoolfile.gz and remove the original. To decompress the file, you can use gunzip:
gunzip mycoolfile.gz
Creating Archives with tar
While gzip is great for compression, it cannot bundle multiple files into a single archive. For that, we use the tar (Tape Archive) utility. A file created with tar is often called a "tarball" and has a .tar extension.
To create a new archive containing multiple files:
tar cvf myarchive.tar file1 file2 directory1
Let's break down the options:
c: create a new archive.v: verbose mode, which lists the files as they are processed.f: file, which specifies that the next argument is the name of the archive file.
The Power of tar and gzip Combined
The true power comes from using tar and gzip together. You can first create a .tar archive and then compress it with gzip, resulting in a .tar.gz file. However, tar provides a convenient way to handle tar compression in a single step using the z option. This process is sometimes referred to as creating a gzip tar archive.
To create a compressed archive, which is a common way to compress tar gz files:
tar czvf myarchive.tar.gz file1 file2 directory1
Here, the z option tells tar to use gzip for compression.
Extracting tar and gzip Archives
To extract files from an archive, you use the x option.
To extract a simple .tar archive:
tar xvf myarchive.tar
To uncompress and extract a .tar.gz archive in one command, simply add the z option again:
tar xzvf myarchive.tar.gz
Let's review the extraction options:
x: extract files from an archive.z: Decompress the archive usingg**z**ip.v: verbose mode, listing files as they are extracted.f: file, specifying the archive file to extract.
A helpful mnemonic for this is: eXtract Zee Very Fast!
tar is a command so essential yet often forgotten. Relevant xkcd: https://xkcd.com/1168
Other Utilities
While tar and gzip are extremely common, you will encounter other archiving and compression formats on your Linux journey. These include bzip2 (which creates .bz2 files and uses the j flag in tar), xz (creating .xz files with the J flag), and the familiar zip/unzip utilities. Each has its own set of commands and compression ratios, but the underlying concepts remain the same.