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 -5This 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 | uniqThe 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 -lEach 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
EasyThe 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
catto readvisitors.txt - Pipes to
sortto arrange names alphabetically - Pipes to
uniqto remove duplicate entries - Pipes to
wc -lto count the unique names
Hint: Chain all four commands together using multiple
|symbols. Remember thatuniqonly 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 -5Data flows left to right through each command in sequence.
To find unique sorted entries:
cat names.txt | sort | uniqThe 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 -lWhen building pipelines, ensure the output format of one command makes sense as input for the next.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline