Bird
Raised Fist0
Linux CLIscripting~15 mins

Why pipes chain commands into workflows in Linux CLI - See It in Action

Choose your learning style10 modes available
Why Pipes Chain Commands into Workflows
📖 Scenario: Imagine you are organizing a big party. You have a list of guests, but you want to invite only those who live in your city and whose names start with the letter 'A'. Instead of checking each guest one by one, you use a simple system that passes the list through different steps automatically. This is like using pipes in Linux commands to filter and process data step by step.
🎯 Goal: You will learn how to use pipes to chain simple Linux commands together. This will help you create workflows that process data smoothly, just like passing a list through different helpers to get the final result.
📋 What You'll Learn
Create a text file with a list of names
Use a command to filter names starting with 'A'
Use another command to sort the filtered names
Use pipes to connect these commands into one workflow
Display the final sorted list of filtered names
💡 Why This Matters
🌍 Real World
In real life, pipes help you process data step by step without creating many temporary files. For example, system administrators use pipes to filter logs, sort data, and find important information quickly.
💼 Career
Understanding pipes is essential for anyone working with Linux or Unix systems. It helps automate tasks, build efficient workflows, and handle large amounts of data easily.
Progress0 / 4 steps
1
Create a text file with a list of names
Create a file called guests.txt with these exact names, each on its own line: Alice, Bob, Anna, Charlie, Andrew, David.
Linux CLI
Hint

Use the echo command with -e to add new lines, and redirect output to guests.txt.

2
Filter names starting with 'A'
Use the grep command with the pattern ^A to find names in guests.txt that start with the letter A. Save the result to a file called filtered.txt.
Linux CLI
Hint

The caret ^ means 'start of line' in grep patterns.

3
Sort the filtered names
Use the sort command to sort the names in filtered.txt alphabetically. Save the sorted names to a file called sorted.txt.
Linux CLI
Hint

The sort command arranges lines alphabetically by default.

4
Use pipes to chain commands and display the result
Use a single command line that uses pipes to filter names starting with A from guests.txt, sort them, and then display the final sorted list on the screen.
Linux CLI
Hint

Use | to connect grep and sort, then just run the command to see the output.

Practice

(1/5)
1. What is the main purpose of using pipes (|) in Linux command lines?
easy
A. To send the output of one command as input to another command
B. To run two commands at the same time independently
C. To save the output of a command to a file
D. To repeat a command multiple times automatically

Solution

  1. Step 1: Understand what pipes do

    Pipes take the output from one command and pass it directly as input to the next command.
  2. Step 2: Compare options with pipe behavior

    Only To send the output of one command as input to another command describes this behavior correctly; others describe different features.
  3. Final Answer:

    To send the output of one command as input to another command -> Option A
  4. Quick Check:

    Pipes connect commands by passing output as input [OK]
Hint: Pipes pass output to next command input [OK]
Common Mistakes:
  • Thinking pipes run commands in parallel independently
  • Confusing pipes with file redirection
  • Assuming pipes repeat commands
2. Which of the following is the correct syntax to use a pipe between two commands in Linux?
easy
A. command1 | command2
B. command1 >| command2
C. command1 || command2
D. command1 & command2

Solution

  1. Step 1: Identify the pipe symbol

    The pipe symbol is a single vertical bar: |.
  2. Step 2: Match syntax with pipe usage

    Only command1 | command2 uses the correct pipe syntax between two commands.
  3. Final Answer:

    command1 | command2 -> Option A
  4. Quick Check:

    Pipe syntax uses a single vertical bar [OK]
Hint: Pipe uses single vertical bar | between commands [OK]
Common Mistakes:
  • Using double pipes || which means OR condition
  • Using >| which is invalid syntax
  • Using & which runs commands in background
3. What will be the output of the following command?
echo "apple banana cherry" | tr ' ' '\n' | sort
medium
A. apple banana cherry
B. apple banana cherry
C. banana apple cherry
D. cherry banana apple

Solution

  1. Step 1: Break down the pipeline

    echo "apple banana cherry" outputs the string. tr ' ' '\n' replaces spaces with new lines, so words become separate lines. sort sorts these lines alphabetically.
  2. Step 2: Determine sorted output

    Words sorted alphabetically are: apple, banana, cherry, each on its own line.
  3. Final Answer:

    apple banana cherry -> Option B
  4. Quick Check:

    Pipe transforms and sorts words line by line [OK]
Hint: Trace each pipe step to find final output [OK]
Common Mistakes:
  • Ignoring that tr changes spaces to new lines
  • Assuming sort does not change order
  • Thinking output stays on one line
4. Identify the error in this pipeline command:
cat file.txt | grep 'error' | | sort
medium
A. sort command cannot be used with pipes
B. Missing filename after cat
C. grep command is misspelled
D. Extra pipe symbol causes syntax error

Solution

  1. Step 1: Check the pipeline syntax

    The command has two pipe symbols | | in a row, which is invalid syntax.
  2. Step 2: Validate other parts

    cat file.txt is correct, grep 'error' is correct, and sort can be used with pipes.
  3. Final Answer:

    Extra pipe symbol causes syntax error -> Option D
  4. Quick Check:

    Double pipes cause syntax errors in pipelines [OK]
Hint: Look for consecutive pipes causing syntax errors [OK]
Common Mistakes:
  • Ignoring extra pipe and blaming grep or cat
  • Thinking sort can't be piped
  • Assuming missing filename error
5. You want to count how many unique users are currently logged in by chaining commands. Which pipeline correctly achieves this?
hard
A. who | cut -d ' ' -f1 | uniq | sort | wc -l
B. who | sort | uniq | cut -d ' ' -f1 | wc -l
C. who | cut -d ' ' -f1 | sort | uniq | wc -l
D. who | uniq | cut -d ' ' -f1 | sort | wc -l

Solution

  1. Step 1: Extract usernames from who output

    cut -d ' ' -f1 extracts the first word (username) from each line.
  2. Step 2: Sort and find unique usernames

    sort orders usernames so uniq can count unique ones correctly.
  3. Step 3: Count unique usernames

    wc -l counts the number of unique usernames output by uniq.
  4. Final Answer:

    who | cut -d ' ' -f1 | sort | uniq | wc -l -> Option C
  5. Quick Check:

    Extract, sort, uniq, then count lines for unique users [OK]
Hint: Sort before uniq to count unique items correctly [OK]
Common Mistakes:
  • Using uniq before sort causing wrong counts
  • Cutting fields after uniq breaking logic
  • Sorting after uniq which is ineffective
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard