Lesson 18: Archiving¶
The collection of many files into a single archive file (often referred to as a tarball) can be achieved using the tar command. Archive files are common in software distribution and backups.
Creating an Archive¶
An archive can be created using the -cf
switch combination. The
command tar
needs to know what to call the archive file, and what
files to archive. It is possible to use a compression library to reduce
the size of the archive. Gzip is
an example compression library which can be invoked by passing the -z
switch. See the manual page for other types of compression.
Let's touch
some additional files, then archive and compress them:
[learner ~]$ cd learning_linux/planets
[learner planets]$ touch earth.{html,xml}
[learner planets]$ tar -czf earth.tar.gz earth.*
[learner planets]$ ls earth*
earth.html earth.tar.gz earth.txt earth.xml
The standard file format for tarballs is .tar
(un-compressed) and
.tar.gz
(compressed using Gzip).
Listing Archive Contents¶
The contents of an existing archive (regardless of compression), can be
listed using the -tf
switch combination.
[learner planets]$ tar -tf earth.tar.gz
earth.html
earth.txt
earth.txt.bak
earth.xml
Extracting Files from an Archive¶
The contents of an existing archive can be extracted using the -xf
switch combination. If the archive has been compressed using Gzip, the
-z
switch needs also to be present. By default, the extraction
destination is the current working directory, but a custom directory can
be provided with the -C
switch.
[learner planets]$ tar -xzf earth.tar.gz -C ..
[learner planets]$ ls ..
earth.html earth.txt earth.txt.bak earth.xml planets