Exercise 0.6: Combining Commands

Using Pipes and Redirection

Author

Chuck Nelson

Published

October 28, 2025

Purpose

Now that you know the basic commands, it’s time to learn how to combine them. In this exercise, you will learn about two of the most powerful features of the shell: redirection (> and >>), which lets you save the output of a command to a file, and pipes (|), which let you use the output of one command as the input for another. This is the key to unlocking complex, automated tasks.

What You’ll Accomplish

By the end of this exercise, you will have successfully:

  • Redirected the output of a command to a new file (>).
  • Appended the output of a command to an existing file (>>).
  • Piped the output of one command into another command (|).

Part 1: Output Redirection

Redirection allows you to control where the output of a command goes. By default, it goes to your screen. Using redirection, you can send it to a file instead.

  • The > operator creates a new file or overwrites an existing file with the output.
  • The >> operator appends the output to the end of an existing file.

Your Task 1: Redirect Output

  1. Navigate to your cli-practice directory.

  2. Use the ls command with redirection to save a list of your files into a new file called file-list.txt.

    # On all operating systems
    ls > file-list.txt
  3. View the contents of the new file. It should contain the output of the ls command.

    cat file-list.txt
  4. Now, append a second command’s output to the same file.

    # On macOS/Linux
    pwd >> file-list.txt
    
    # On Windows
    cd >> file-list.txt
  5. View the file again. You will see the output of the pwd command has been added to the end.

Part 2: Pipes

The pipe | is one of the most powerful ideas in the command line. It lets you chain commands together, creating a pipeline where the output of one command becomes the input of the next.

Your Task 2: Pipe Commands

A common use case is to filter a long output. Let’s find a specific file in a long list.

On macOS and Linux, the grep command is used to find text within an output.

# Create some files first
touch apple.txt
touch banana.txt
touch apricot.txt

# Pipe the output of ls into grep to find only the files with "apple"
ls | grep "apple"

Output: apple.txt

On Windows, the findstr command serves a similar purpose to grep.

# Create some files first
New-Item apple.txt
New-Item banana.txt
New-Item apricot.txt

# Pipe the output of ls into findstr
ls | findstr "apple"

Output: apple.txt

In this pipeline, ls runs first and produces a list of all files. That list is then “piped” as input to the grep or findstr command, which filters the list and only prints the lines that match the search term.

Verification Challenge

Let’s combine redirection and pipes.

Your Task: Write a single command that lists all the files in your cli-practice directory, filters that list to show only the files containing the word “apple”, and saves that filtered list to a new file called apple-files.txt.

Hint: You will need to use both a | and a > in the same line.

Reflect and Review

ImportantReflection

In your Microsoft Teams Student Notebook, answer the following:

  • What is the difference between the > and >> redirection operators?
  • In your own words, what does the pipe | operator do?
  • Think of a scenario where piping ls into grep/findstr might be more useful than just running ls by itself.
Back to top