Archiving and Compression
Archiving and Compression
Archiving and Compression
Objective Description
Archiving files in the user home directory
Introduction
Introduction
● In this chapter, we discuss how to manage archive files at the command
line.
● File archiving is used when one or more files need to be transmitted or
stored as efficiently as possible.
● There are two fundamental aspects which this chapter explores:
○ Archiving: Combines multiple files into one, which eliminates the overhead in
individual files and makes it easier to transmit.
○ The file is compressed by invoking the gzip command with the name of the file as the
argument.
○ After that command completes, the original file is gone, and a compressed version with a
file extension of .gz is left in its place.
● Compressed files can be restored to their original form (decompression) using either
the gunzip command or the gzip –d command.
● After gunzip does its work, the longfile.txt file is restored to its original size
and file name:
sysadmin@localhost:~/Documents$ gunzip longfile.txt.gz
sysadmin@localhost:~/Documents$ ls -l longfile*
-rw-r--r-- 1 sysadmin sysadmin 66540 Dec 20 2017 longfile.txt
Archiving
Archiving Files
● Archiving is when you compress many files or directories into one file.
● The traditional UNIX utility to archive files is called tar, which is a short
form of TApe aRchive.
● Tar has three modes that are helpful to become familiar with:
○ Create: Make a new archive out of a series of files.
● Creating an archive with the tar command requires two named options:
-c Create an archive.
-f ARCHIVE Use archive file. The argument ARCHIVE will be the name of the resulting archive file.
● The following example shows a tar file, also called a tarball, being created from
multiple files:
sysadmin@localhost:~/Documents$ tar -cf alpha_files.tar alpha*
sysadmin@localhost:~/Documents$ ls -l alpha_files.tar
-rw-rw-r-- 1 sysadmin sysadmin 10240 Oct 31 17:07 alpha_files.tar
Archiving Files - Create Mode
● Tarballs can be compressed for easier transport, either by using gzip on the archive
or by having tar do it with the -z option:
● The bzip2 compression can be used instead of gzip by substituting the -j option
for the -z option and using .tar.bz2, .tbz, or .tbz2 as the file extension:
● Given a tar archive, compressed or not, you can see what’s in it by using the -
t option. The next example uses three options:
● You can extract the archive with the –x option once it’s copied into a different
directory. The following example uses the similar pattern as with the other modes:
● The zip command will not recurse into subdirectories by default (tar does), so you
must use the –r option to indicate recursion is to be used.
ZIP Files
● The –l list option of the unzip command lists files in .zip archives:
sysadmin@localhost:~/Documents$ unzip -l School.zip
Archive: School.zip
Length Date Time Name
--------- ---------- ----- ----
0 2017-12-20 16:46 School/
0 2018-10-31 17:47 School/Engineering/
● Just like tar, you can pass filenames on the command line.