CIS120 Linux Fundamentals by Scott Shaper

Linux Pipelines

Think of Linux pipelines like a factory assembly line. Just as raw materials move from one station to another, getting transformed at each step, data flows through a series of commands, getting processed and transformed along the way. The pipe symbol (|) is like the conveyor belt that moves the data from one command to the next.

The examples below use the course setup. Work in ~/playground/chapter3, where the setup has created error_log.txt, error_report.txt, normal_file.txt, script1.py, script2.py, script3.py, file.txt, users.txt, access.log, app.log, server.log, and data.csv. Use cd ~/playground/chapter3 before trying the commands.

When to Use Pipelines

Use pipelines when you want to:

Basic Pipeline Concepts

A pipeline connects commands using the pipe symbol (|). The output of the command on the left becomes the input for the command on the right. Think of it like a chain of commands, where each link processes the data in some way.

Common Pipeline Patterns

Pattern What It Does When to Use It
command1 | command2 Basic two-command pipeline Simple data processing
command1 | command2 | command3 Three-command pipeline Complex data processing
command1 | tee file.txt Save and display output When you need both output and a file

Practical Examples

In some of these examples we are using commands that we have not yet covered. We will cover them in later chapters.

File Operations

cd ~/playground/chapter3

# Find all text files containing "error"
ls *.txt | grep "error"

# Count how many Python files are in the directory
ls *.py | wc -l

# Sort files by size
ls -l | sort -k5 -n

Text Processing

# Convert text to uppercase and count lines
cat file.txt | tr '[:lower:]' '[:upper:]' | wc -l

# Extract usernames from users.txt (similar to /etc/passwd format)
cat users.txt | cut -d: -f1 | sort

# Filter data.csv to rows for Engineers, take the name (first field, comma-delimited), then list sorted unique names
#NOTE a csv file is a text file with comma-separated values.
grep Engineer data.csv | cut -d, -f1 | sort -u

Tips for Success

Common Mistakes to Avoid

Best Practices