Skip to content

Day 16 - Archiving and compressing

INTRO

As a system administrator, you need to be able to confidently work with compressed “archives” of files. In particular two of your key responsibilities; installing new software, and managing backups, often require this.

YOUR TASKS TODAY

  • Create a tarball
  • Create a compressed tarball and compare sizes
  • Extract files from a tarball

CREATING ARCHIVES

On other operating systems, applications like WinZip, and pkzip before it, have long been used to gather a series of files and folders into one compressed file - with a .zip extension. Linux takes a slightly different approach, with the “gathering” of files and folders done in one step, and the compression in another.

So, you could create a “snapshot” of the current files in your /etc/init.d folder like this:

tar -cvf myinits.tar /etc/init.d/

This creates myinits.tar in your current directory.

Note 1: The -f switch specifies that “the output should go to the filename which follows” - so in this case the order of the switches is important. VERY IMPORTANT: tar considers anything after -f as the name of the archive that needs to be created. So, we should always use -f as the last flag while creating an archive.

Note 2: The -v switch (verbose) is included to give some feedback - traditionally many utilities provide no feedback unless they fail.

(The cryptic “tar” name? - originally short for “tape archive”)

You could then compress this file with GnuZip like this:

gzip myinits.tar

…which will create myinits.tar.gz. A compressed tar archive like this is known as a “tarball”. You will also sometimes see tarballs with a .tgz extension - at the Linux commandline this doesn’t have any meaning to the system, but is simply helpful to humans.

In practice you can do the two steps in one with the “-z” switch, like this:

tar -cvzf myinits.tgz /etc/init.d/

This uses the -c switch to say that we’re creating an archive; -v to make the command “verbose”; -z to compress the result - and -f to specify the output file.

TASKS FOR TODAY

  • Check the links under “Resources” to better understand this - and to find out how to extract files from an archive!
  • Use tar to create an archive copy of some files and check the resulting size
  • Run the same command, but this time use -z to compress - and check the file size
  • Copy your archives to /tmp (with: cp) and extract each there to test that it works

POSTING YOUR PROGRESS

Nothing to post today - but make sure you understand this stuff, because we’ll be using it for real in the next day’s session!

EXTENSION

  • What is a .bz2 file - and how would you extract the files from it?
  • Research how absolute and relative paths are handled in tar - and why you need to be careful extracting from archives when logged in as root
  • You might notice that some tutorials write “tar cvf” rather than “tar -cvf” with the switch character - do you know why?

RESOURCES

Some rights reserved. Check the license terms here

Comments