Using Temporary Files and Directories in Linux

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 working with temporary files and directories. Knowing how and when to use temporary files and directories for transient and temporary storage is a good skill to have as a systems administrator.

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.

Using Temporary Files and Directories

Introduction

In this hands-on lab, we will practice working with temporary files and directories. Knowing how and when to use temporary files and directories for transient and temporary storage is a good skill to have as a systems administrator.

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.

Retrieve and Unpack the Archive

  1. List the current working directory.
    pwd
  2. Change to the /tmp directory.
    cd /tmp
  3. Use wget to retrieve the file.
    wget https://github.com/mscjr/LA/raw/master/RH342/sosreport-ip-10-0-1-11-2019-03-22-wxoxhnk.tar.xz
  4. List the contents of the /tmp directory.
    ll
  5. Unpack the archive.
    tar xf sosreport-ip-10-0-1-11-2019-03-22-wxoxhnk.tar.xz
  6. Verify that this was successful.
    ll

Create a Temporary File from the Directory Listing

  1. Create a temporary file.
    tmp_file=$(mktemp)
  2. Verify that this was successful.
    echo $tmp_file
  3. Create a temporary file from a recursive directory listing of the archive folder.
    ls -R sosreport-ip-10-0-1-11-2019-03-22-wxoxhnk/ >> $tmp_file
  4. Verify that this was successful.
    cat $tmp_file

Move the version.txt File to a More Persistent Temporary Directory

  1. Change to the sosreport-ip-10-0-1-11-2019-03-22-wxoxhnk folder.
    cd sosreport-ip-10-0-1-11-2019-03-22-wxoxhnk
  2. List the contents of the folder.
    ls
  3. Change back to the /tmp directory.
    cd ..
  4. Move the version.txt file to /var/tmp.
    mv sosreport-ip-10-0-1-11-2019-03-22-wxoxhnk/version.txt /var/tmp
  5. Verify that this was successful.
    ll /var/tmp/version.txt

Conclusion

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