Menu
Coddy logo textTech

Chaining Multiple Commands

Part of the Fundamentals section of Coddy's Terminal journey — lesson 45 of 82.

You're not limited to connecting just two commands. Pipes can chain as many commands as you need, with each one processing the output of the previous command.

Simply add more | symbols to extend the pipeline:

cat data.txt | sort | head -5

This reads the file, sorts all lines alphabetically, then shows only the first 5 results. Data flows left to right through each command in sequence.

Here's a practical example that finds unique sorted entries:

cat names.txt | sort | uniq

The file contents get sorted first (which groups duplicates together), then uniq removes consecutive duplicate lines. The order matters here — uniq only removes adjacent duplicates, so sorting must come first.

You can add even more stages. To count how many unique names exist:

cat names.txt | sort | uniq | wc -l

Each pipe adds another transformation step. When building longer pipelines, think about what each command needs as input and what it produces as output. The output format of one command must make sense as input for the next.

challenge icon

Challenge

Easy

The file visitors.txt contains a list of visitor names with duplicates. Use a pipeline with three commands to count how many unique visitors there are.

Run a single command that:

  • Uses cat to read visitors.txt
  • Pipes to sort to arrange names alphabetically
  • Pipes to uniq to remove duplicate entries
  • Pipes to wc -l to count the unique names

Hint: Chain all four commands together using multiple | symbols. Remember that uniq only removes adjacent duplicates, so sorting must come before it.

Cheat sheet

Pipes can chain multiple commands together, with each command processing the output of the previous one. Use the | symbol to connect commands:

cat data.txt | sort | head -5

Data flows left to right through each command in sequence.

To find unique sorted entries:

cat names.txt | sort | uniq

The uniq command removes consecutive duplicate lines, so sorting must come first to group duplicates together.

To count unique entries, add wc -l to the pipeline:

cat names.txt | sort | uniq | wc -l

When building pipelines, ensure the output format of one command makes sense as input for the next.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals