Skip to content

Use Linux Pipes and Redirection in the Shell

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.


Step 1: Create a Small Sample File

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
❯ View Expected Console Output
INFO service started
WARN retrying request
ERROR database connection failed
INFO retry succeeded
ERROR request timed out

Step 2: Filter and Count Matching Lines

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
❯ View Expected Console Output
2
ERROR database connection failed
ERROR request timed out

Step 3: Save Output Instead of Printing It

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
❯ View Expected Console Output
3 /home/sam/linux-basics-lab/errors.txt
WARN retrying request

Step 4: Separate Standard Output and Errors

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
❯ View Expected Console Output
Normal results and error messages are saved in the selected report files.

Step 5: Make a Pipeline Fail When an Earlier Command Fails

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
❯ View Expected Console Output
ERROR database connection failed
ERROR request timed out
Pipeline exit status: 0

Comments