03. Archiving and Compressing Multiple Files tar, zip, unzip

To compress folders and multiple files for transferring between computers in one step, we use the tar command. The .tar format is used for collecting multiple files into one archive file for distribution or backup.

Name origin

"tar" is short for tape archiving utility. The name is a reminescence of when file were backed up on and occasionally retrieved from magenetic tape, which was then used as a storage device.

Furthermore, the term tarball is a jargon term describing "a bunch of files stuck together in a ball of tar."

Options

Options for tar don't need a hyphen (-) preceding it. Here are a list of common action options.

c
Create an archive. List the list files and/or directories as arguments.
r
Append specified pathnames to the end of an archive.
t
List the contents of an archive.
x
Extract.

With an action option you may include a qualifier.

v
Verbose mode.
f
Specify the name of the .tar file you want to create.
P
Retain the leading / for filenames.
z
Process through gzip.
j
Process through bzip2.

Some common commands you will use are:

tar -tf fileName
List
tar -xf fileName
Extract
tar -cf fileName
Create
tar --help
Help

Examples with tar

Here are some examples with tar that you'll most likely come across.

1) Creating a tar file

$ tar -cvf myArchive.tar README.md todo.txt index.html
a README.md
a index.html
a todo.txt
# Created myArchive.tar file

2) Unpacking tar files

$ tar xvf archive.tar

3) List contents with table-of-contents mode

Before you go and extract a .tar file, you may want to have a peek inside. The tar file allows for this with the table-of-contents mode. Simply pass in the -t option.

4) Preserving original permissions

If you extract the files, the original permission settings may be overwritten with your umask settings. To preserve the original permissions, use the -p option.

5) Decompressing .tar.gz files

Oftentimes you'll come across files with a .tar.gz extension. To unpack this, simply use the gunzip command first, the tar.

$ gunzip myArchive.tar.gz
$ tar xf myArchive.tar
# Or, even faster...
$ zcat myArchive.tar.gz | tar xvf

zcat is the same as using gunzip with the -c option, which outputs to standard out.

A Windows comparison

If you're working with a Windows user, you may come across with the .zip compression file extension. The command line can also handle zip files in a mannner similar to tar files.

Zipping

Zipping is as easy to use as the gzip command. There are two types of command modes that options work in - external and internal. Internal modes (delete and copy) operate exclusively on entries in an existing archive, while external modes (add, update and freshen) read from both files from the file system, and existing archives.

add
Update existing entries and add new files. If archive does not exist, create it.
-d
Delete select entries.
-f
Freshen. Update existing entries if newer on the file system. Does not add any new files to the archive.
-r
Recursively zip (include ones in subdirectories).
-u
Update existing entries, and add new files.
$ zip myArchive README.md todo.txt index.html
adding: README.md (deflated 90%)
adding: index.html (deflated 60%)
adding: todo.txt (deflated 61%)

Unzipping

To unzip, use the unzip command.

$ unzip myArchive.zip

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad