Skip to main content

Archiving Files on the Command Line

Hands-On Lab

 

Photo of Michael Christian

Michael Christian

Course Development Director in Content

Length

00:45:00

Difficulty

Beginner

In this hands-on lab, we will practice archiving and compression. Archiving is often used for backing up and moving files, and the ability to create compressed archives is a fundamental skill for a systems architect. By the end of this lab, you will be familiar with creating and working with archives.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Archiving Files on the Command Line

Introduction

In this hands-on lab, we will practice archiving and compression. Archiving is often used for backing up and moving files, and the ability to create compressed archives is a fundamental skill for a systems architect. By the end of this lab, you will be familiar with creating and working with archives.

Connecting to the Lab

  1. Open your terminal application, and run the following command (remember to replace <PUBLIC_IP> with the public IP you were provided on the lab instructions page):
    ssh cloud_user@<PUBLIC_IP>
  2. Enter yes at the prompt.
  3. Enter your cloud_user password at the prompt.

Create a Normal Archive

  1. Determine the current working directory.
    pwd
  2. List the contents of the current working directory.
    ls
  3. Create an uncompressed archive of the ~/Practice folder.
    tar cf archive.tar Practice/

Add a File to the Archive

  1. Add the file ~/extra.txt to archive.tar.
    tar rf archive.tar extra.txt
  2. Verify that this was successful.
    tar tvf archive.tar | grep extra.txt

Create a Compressed Archive

  1. Run the following command:
    tar czf archive.tgz Practice/
  2. Do a long listing, and compare the sizes of the compressed and uncompressed archive files.
    ll

Compress the Normal Archive

  1. Use the gzip utility to compress the first tarball with maximum compression.
    gzip -9 archive.tar
  2. Do a long listing, and compare the sizes of the two compressed archive files.
    ll

Extract a Single File from an Archive

  1. Run the following command to extract the file Practice/Test/version.txt from the compressed archive archive.tar.gz:
    tar xzf archive.tar.gz Practice/Test/version.txt
  2. Do a long listing on the current directory.
    ll
  3. Do a long listing on Practice/Test/.
    ll
  4. Remove version.txt from Practice/Test.
    rm Practice/Test/version.txt
  5. Verify that this was successful.
    ll Practice/Test/
  6. Run the extraction command again.
    tar xzf archive.tar.gz Practice/Test/version.txt
  7. Verify that this was successful.
    ll Practice/Test/

Conclusion

Congratulations, you've successfully completed this hands-on lab!