Home

Pipes

Pipes are a fundamental concept in Unix and Unix-like operating systems. They allow you to chain commands together, using the output of one command as the input for another. Let's break it down:

Basic Concept:

A pipe is represented by the vertical bar character '|'. It takes the standard output (stdout) of one command and feeds it as standard input (stdin) to another command.

Syntax:

command1 | command2

This means "run command1 and send its output to command2".

How it works:

  1. When you use a pipe, the shell creates a temporary pipe file in memory (not on disk).
  2. The output of command1 is written to this pipe.
  3. command2 reads from this pipe as its input.
  4. This happens with no intermediate files stored on disk.

Examples:

a. List files and search for a specific name: ls | grep "example"

b. Count the number of files in a directory: ls | wc -l

c. Sort the output of a command: cat file.txt | sort

Common use with text processing:

Pipes are often used with commands like grep, sed, awk, sort, and uniq for text processing tasks.

Learning about pipes is a fundamental step in developing effective command-line skills for Unix-like environments. They embody the Unix philosophy of creating small, focused tools that can be combined to perform complex tasks.

© Mike Surowiec