Skip to content

Use Linux Pipes and Redirection in the Shell: A Beginner Guide

Admin
October 4, 2026
6 min read
Beginner
💻 Target OS: Ubuntu 24.04+ / Debian 12+ / RHEL 9+

Before you begin

  • Applies to:Linux shell exercises for passing output between commands and writing it to files.
  • Where to start:Open a terminal as your regular user and use the practice files in your home directory.
  • What you need:Have a writable home directory. Check a destination filename before using > because it replaces existing contents.
Guide tools
Guide tools
0/0 Steps (0%)

Pipes and redirection let you connect small shell commands into a useful workflow. A pipe passes one command’s output to another command; redirection sends output or errors to a file. These techniques are useful for inspecting logs, preparing reports, and saving command results.

The examples below work with a sample file in your home directory. A single > replaces a file’s contents, so use it only when you intend to overwrite the target.


01

Prepare Example Log Lines

Practice Data

Create a sample log file with a here-document. The quoted EOF marker tells the shell to write the lines literally, without expanding special characters or variables inside them.

Terminal window
mkdir -p ~/linux-basics-lab
cat > ~/linux-basics-lab/app.log <<'EOF'
INFO service started
WARN retrying request
ERROR database connection failed
INFO retry succeeded
ERROR request timed out
EOF
cat ~/linux-basics-lab/app.log
❯ Illustrative Output (Results Vary)
INFO service started
WARN retrying request
ERROR database connection failed
INFO retry succeeded
ERROR request timed out

02

Pipe Search Results into Another Command

Pipelines

The pipe character | sends standard output from the command on its left to the command on its right. Here, grep selects matching lines and wc -l counts them. Use grep -i when matching should ignore letter case.

Terminal window
grep -i 'error' ~/linux-basics-lab/app.log | wc -l
grep -i 'error' ~/linux-basics-lab/app.log | sort
❯ Illustrative Output (Results Vary)
2
ERROR database connection failed
ERROR request timed out

03

Write a New Report or Append to It

Output Files

Use > to create or replace a file, and >> to append to its end. The tee command both displays output and writes it to a file; use tee -a to append instead of replacing.

Terminal window
grep -i 'error' ~/linux-basics-lab/app.log > ~/linux-basics-lab/errors.txt
date -Is >> ~/linux-basics-lab/errors.txt
wc -l ~/linux-basics-lab/errors.txt
grep -i 'warn' ~/linux-basics-lab/app.log | tee ~/linux-basics-lab/warnings.txt
❯ Illustrative Output (Results Vary)
3 /home/sam/linux-basics-lab/errors.txt
WARN retrying request

04

Capture Errors Without Hiding Normal Results

Diagnostics

Shell commands normally use file descriptor 1 for standard output and 2 for standard error. Redirect them separately when you need both. The order in 2>&1 matters: this sends standard error to wherever standard output is currently going.

Terminal window
find ~/linux-basics-lab -type f -name '*.log' \
> ~/linux-basics-lab/find-results.txt \
2> ~/linux-basics-lab/find-errors.txt
# Combine standard output and standard error in one file.
find ~/linux-basics-lab -type f -name '*.log' \
> ~/linux-basics-lab/find-all.txt 2>&1
❯ Illustrative Output (Results Vary)
Normal results and error messages are saved in the selected report files.

05

Enable pipefail in a Bash Session

Reliable Scripts

By default, a pipeline usually returns the exit status of its final command. In Bash, set -o pipefail makes the pipeline return a failure if an earlier command fails too. This matters in scripts where later commands might succeed even though the data-producing command did not.

Terminal window
bash
set -o pipefail
grep -i 'ERROR' ~/linux-basics-lab/app.log | sort | tee ~/linux-basics-lab/errors-sorted.txt
echo "Pipeline exit status: $?"
exit
❯ Illustrative Output (Results Vary)
ERROR database connection failed
ERROR request timed out
Pipeline exit status: 0

Comments