Skip to main content

Command Piping and Redirection

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 use piping and redirection to populate a file with the specific results of commands. Understanding piping and redirection takes practice, and in this lab you will practice both to populate a file using redirection with data process with piping. By the end of this lab, you should be comfortable piping output from one command to another, and redirecting the output to a file.

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.

Command Piping and Redirection

Introduction

In this hands-on lab, we will use piping and redirection to populate a file with the specific results of commands.

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>

Determine the Number of Files and Folders in /usr/share

  1. List the current working directory.
    pwd
  2. List the contents of the home directory.
    ls
  3. List the contents of the /usr/share directory.
    ls /usr/share/
  4. Determine the number of files and folders in the /usr/share/ directory.
    ls /usr/share/ | wc -l
  5. Rerun the previous command, and send the output to the value.txt file.
    ls /usr/share/ | wc -l > ~/value.txt
  6. List the contents of the current working directory.
    ls
  7. List the contents of the value.txt file.
    cat value.txt

Determine the Number of Unpacked Entries in /var/log/dpkg.log

  1. List all of the unpacked entries in the directory.
    cat /var/log/dpkg.log | grep unpacked
  2. Determine the number of unpacked entries in the directory.
    cat /var/log/dpkg.log | grep unpacked | wc -l
  3. Rerun the previous command, and send the output to the value.txt file.
    cat /var/log/dpkg.log | grep unpacked | wc -l >> value.txt
  4. Verify that this was successful.
    cat value.txt

Determine the Total Number of Entries in /var/log/dpkg.log

  1. Run the following command:
    cat /var/log/dpkg.log | wc -l
  2. Rerun the previous command, and send the output to the value.txt file.
    cat /var/log/dpkg.log | wc -l >> value.txt
  3. Verify that this was successful.
    cat value.txt

Conclusion

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