Use Linux Pipes and Redirection in the Shell: A Beginner Guide
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
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.
Prepare Example Log Lines
Practice DataCreate 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.
mkdir -p ~/linux-basics-labcat > ~/linux-basics-lab/app.log <<'EOF'INFO service startedWARN retrying requestERROR database connection failedINFO retry succeededERROR request timed outEOFcat ~/linux-basics-lab/app.log❯ Illustrative Output (Results Vary)
INFO service startedWARN retrying requestERROR database connection failedINFO retry succeededERROR request timed outPipe Search Results into Another Command
PipelinesThe 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.
grep -i 'error' ~/linux-basics-lab/app.log | wc -lgrep -i 'error' ~/linux-basics-lab/app.log | sort❯ Illustrative Output (Results Vary)
2ERROR database connection failedERROR request timed outWrite a New Report or Append to It
Output FilesUse > 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.
grep -i 'error' ~/linux-basics-lab/app.log > ~/linux-basics-lab/errors.txtdate -Is >> ~/linux-basics-lab/errors.txtwc -l ~/linux-basics-lab/errors.txtgrep -i 'warn' ~/linux-basics-lab/app.log | tee ~/linux-basics-lab/warnings.txt❯ Illustrative Output (Results Vary)
3 /home/sam/linux-basics-lab/errors.txtWARN retrying requestCapture Errors Without Hiding Normal Results
DiagnosticsShell 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.
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.Enable pipefail in a Bash Session
Reliable ScriptsBy 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.
bashset -o pipefailgrep -i 'ERROR' ~/linux-basics-lab/app.log | sort | tee ~/linux-basics-lab/errors-sorted.txtecho "Pipeline exit status: $?"exit❯ Illustrative Output (Results Vary)
ERROR database connection failedERROR request timed outPipeline exit status: 0Your feedback keeps these production procedures accurate and battle-tested.