LinuxTutorial2 min read

How to Use Pipes and Redirects on Linux

Understand Linux pipes and I/O redirection — chain commands with |, save output to files with > and >>, and redirect errors with 2>&1.

Developer terminal on a laptop in low light

Standard Streams

Every Linux command works with three streams:

  • stdin (0) — input (keyboard by default)
  • stdout (1) — normal output (terminal by default)
  • stderr (2) — error messages (terminal by default)

Pipes and redirects let you control where these streams go.

The Pipe | — Chain Commands

A pipe sends the stdout of one command as the stdin of the next:

ls -la | grep ".log"                # filter ls output
ps aux | grep nginx                 # find nginx processes
cat access.log | grep "404" | wc -l # count 404 errors
history | tail -20                  # see last 20 commands

You can chain as many pipes as you need.

Output Redirection > and >>

echo "hello" > file.txt         # write to file (overwrite)
echo "world" >> file.txt        # append to file
ls -la > listing.txt            # save ls output to a file
date > timestamp.txt            # save current date to a file

Warning: > overwrites the file without asking. >> appends safely.

Input Redirection <

sort < names.txt                # sort reads from file instead of keyboard
wc -l < report.txt              # count lines in a file

Redirect stderr (Error Messages)

find / -name "*.conf" 2>/dev/null   # discard error messages
command 2> errors.log               # save errors to a file
command > output.log 2>&1           # save both stdout and stderr to one file
command &> all.log                  # shorthand for above (bash only)

The 2>&1 syntax means "redirect file descriptor 2 (stderr) to wherever file descriptor 1 (stdout) currently goes".

Combining Redirects

# Run a command, save all output to a log, and still show it on screen
./build.sh 2>&1 | tee build.log

# Append output to a log file
./backup.sh >> /var/log/backup.log 2>&1

/dev/null — The Black Hole

/dev/null is a special file that discards everything written to it. Use it to suppress output you don't care about:

command > /dev/null              # discard stdout
command 2>/dev/null              # discard stderr
command &>/dev/null              # discard everything

Practical Examples

# Find the 10 largest files under /var
du -sh /var/* 2>/dev/null | sort -rh | head -10

# Count unique IP addresses in an access log
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -20

# Save a timestamped backup of a config
cp /etc/nginx/nginx.conf "/etc/nginx/nginx.conf.$(date +%F).bak"