Linux Exercise: Administrative Commands

tar

By now you should have created a series of files and directories in your home directory. We are going to use both the tar and the cpio command to make a backup of your whole home directory.

  1. Make a long, recursive list of all files in your home directory. Take a look at the owner, group and permissions of the files.
    • $ cd
    • $ ls -laR
  2. Use the du command to find out how many megabytes your directory is currently consuming.
    • $ du -m
  3. Create a backup of your home directory using tar. Store the backup in /tmp, making sure that the filename is unique.
    • $ tar -cvf /tmp/yourname.tar .
  4. Look at your backup file. How big is it?
    • $ ls -l /tmp/yourname.tar
      The file should be approximately the same size as the output of the du command you ran earlier.
  5. Verify the contents of your backup file.
    • $ tar -tvf /tmp/yourname.tar
  6. Recursively delete all files and directories, including the hidden files and directories, from your home directory. Verify that your directory is now totally empty.
    • $ rm -fr * .[!.]*
    • $ ls -laR
    • $ du -m
  7. Restore the contents of your backup file. Verify that your directory is now filled with all your data again.
    • $ tar -xvf /tmp/yourname.tar
    • $ ls -laR
    • $ du -m

cpio (optional)

cpio is not used often on Linux. However, it is an LPI requirement to know how to work with cpio. Do this part of the exercise only if you need knowledge of, and practice with cpio.

  1. Create a backup of your home directory using cpio. Store the backup in /tmp, making sure that the filename is unique.
    • $ find . | cpio -o > /tmp/yourname.cpio
  2. Look at your backup file. How big is it?
    • $ ls -l /tmp/yourname.cpio
      The file should be approximately the same size as the output of the du command you ran earlier.
  3. Verify the contents of your backup file.
    • $ cpio -itv < /tmp/yourname.cpio
  4. Recursively delete all files and directories, including the hidden files and directories, from your home directory. Verify that your directory is now totally empty.
    • $ rm -fr * .[!.]*
    • $ ls -laR
    • $ du -m
  5. Restore the contents of your backup file. Verify that your directory is now filled with all your data again.
    • $ cpio -iv < /tmp/yourname.cpio
    • $ ls -laR
    • $ du -m

Compression utilities

Depending on the exact release date of your system, some or all of the following compression utilities may be present:

  • compress/uncompress
  • gzip/gunzip
  • bzip2/bunzip2
  • xz/unxz

You will perform the same sequence of commands with all these utilities. If any of the utilities is not present, simply skip that step.

  1. Using compress, compress your backup tar file. Use the time command to show how long the compression took. Look at the size of the resulting file to see how effective the compression was. Use the appropriate tar command to list the contents of the file. Then uncompress the file again.
    • $ time compress /tmp/yourname.tar
    • $ ls -l /tmp/yourname.tar.Z
    • $ tar -tZvf /tmp/yourname.tar.Z
    • $ uncompress /tmp/yourname.tar.Z
  2. Perform the same steps, but this time use gzip.
    • $ time gzip /tmp/yourname.tar
    • $ ls -l /tmp/yourname.tar.gz
    • $ tar -tzvf /tmp/yourname.tar.gz
    • $ gunzip /tmp/yourname.tar.gz
  3. Perform the same steps, but this time use bzip2.
    • $ time bzip2 /tmp/yourname.tar
    • $ ls -l /tmp/yourname.tar.bz2
    • $ tar -tjvf /tmp/yourname.tar.bz2
    • $ bunzip2 /tmp/yourname.tar.bz2
  4. Perform the same steps, but this time use xz.
    • $ time xz /tmp/yourname.tar
    • $ ls -l /tmp/yourname.tar.xz
    • $ tar -tJvf /tmp/yourname.tar.xz
    • $ unxz /tmp/yourname.tar.xz

Checksumming commands

  1. Create a small text file, with a one line comment "this is a test file" in it.
    • $ vi testfile
      Insert the line "this is a test file", then save the file.
  2. Using utilities such as sum, md5sum, sha256sum and sha512sum, create the checksum of the file.
    • $ sum testfile
    • $ md5sum testfile
    • $ sha256sum testfile
    • $ sha512sum testfile
  3. Make a small change to your test file, for instance by adding an additional character.
    • $ echo "" >> testfile
  4. Create the checksums again. Did you notice a change?
    • $ sum testfile
    • $ md5sum testfile
    • $ sha256sum testfile
    • $ sha512sum testfile
End of exercise