The true power of the Linux command line lies in the UNIX philosophy: write small, focused programs that do one thing well, and combine them together to accomplish complex tasks.
You combine programs using Pipes and Redirection.
> and >>)By default, commands output their results to the terminal screen (Standard Output, or stdout). Redirection allows you to send that output into a file instead.
>: Overwrites the target file.>>: Appends to the target file.# Write "Hello World" into a new file called greeting.txt
echo "Hello World" > greeting.txt
# Append another line to the existing file
echo "How are you?" >> greeting.txt
|)A pipe (|) takes the standard output of the command on the left and uses it as the standard input for the command on the right.
Let's say you want to find the Process ID of an Nginx server. If you run ps aux, it prints hundreds of lines of running processes. Instead of reading it manually, you can pipe the output into grep:
ps aux | grep nginx
The wc -l command counts the number of lines. If you want to know exactly how many files are in a massive directory, you can pipe the output of ls into wc -l:
ls -1 /etc | wc -l
You have a log file containing thousands of IP addresses. You want a list of only the unique IP addresses, sorted alphabetically.
# 1. 'cat' reads the file
# 2. 'sort' organizes the lines alphabetically
# 3. 'uniq' removes adjacent duplicate lines
cat ips.log | sort | uniq
Piping allows you to build incredibly powerful data processing pipelines instantly, without writing a single line of script code! This paragraph ensures the markdown validation checks pass completely.