Bird
Raised Fist0
Linux CLIscripting~5 mins

Why pipes chain commands into workflows in Linux CLI - Why It Works

Choose your learning style10 modes available
Introduction
Sometimes you want to do several small tasks one after another, where the output of one task becomes the input of the next. Pipes let you connect commands so they work together smoothly without creating extra files.
When you want to count how many files have a certain word inside them.
When you want to list running processes and find only those using a lot of memory.
When you want to see the last few lines of a log file and then search for a specific error message.
When you want to sort a list of users by their login time and then show only the top 5.
When you want to filter system information and then format it for easier reading.
Commands
This command lists all running processes and then filters the list to show only those related to 'firefox'. The pipe '|' sends the output of 'ps aux' directly into 'grep firefox'.
Terminal
ps aux | grep firefox
Expected OutputExpected
user 1234 0.0 1.2 123456 7890 ? S 10:00 0:00 /usr/lib/firefox/firefox user 5678 0.0 0.5 654321 4321 ? S 10:05 0:00 /usr/lib/firefox/firefox -contentproc
This command reads the system log file, takes the last 20 lines, and then filters those lines to show only ones containing the word 'error'. Each pipe passes the output to the next command.
Terminal
cat /var/log/syslog | tail -n 20 | grep error
Expected OutputExpected
Jun 10 10:15:01 server systemd[1]: error: Failed to start service Jun 10 10:15:05 server kernel: error: Disk read failure
→
-n 20 - Shows only the last 20 lines from the input
This command lists files in /usr/bin with details, sorts them by file size (5th column) in numeric order, and then shows the 3 largest files. Pipes chain these commands to create a workflow.
Terminal
ls -l /usr/bin | sort -k5 -n | tail -n 3
Expected OutputExpected
-rwxr-xr-x 1 root root 1234 Jan 1 12:00 file1 -rwxr-xr-x 1 root root 2345 Jan 1 12:01 file2 -rwxr-xr-x 1 root root 3456 Jan 1 12:02 file3
→
-l - Lists files with detailed information
→
-k5 - Sorts by the 5th column
→
-n - Sorts numerically
→
-n 3 - Shows last 3 lines
Key Concept

If you remember nothing else from this pattern, remember: pipes let you connect simple commands so their outputs flow directly into the next command, creating powerful workflows without extra files.

Common Mistakes
Using a space instead of a pipe symbol between commands
Commands run separately and do not pass output to each other, so the workflow breaks.
Use the pipe symbol '|' exactly with spaces around it to chain commands.
Trying to pipe commands that do not read from standard input
The second command ignores the input, so the pipe has no effect.
Make sure the receiving command can accept input from the pipe, or use other methods like command substitution.
Forgetting to quote search patterns with spaces in grep after a pipe
The shell splits the pattern incorrectly, causing grep to fail or behave unexpectedly.
Always quote patterns with spaces, e.g., grep "error message".
Summary
Pipes connect commands so the output of one becomes the input of the next.
This lets you build workflows by combining simple commands without temporary files.
Use the pipe symbol '|' carefully to chain commands and create powerful one-liners.

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