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
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❯ View Expected Console Output
INFO service startedWARN retrying requestERROR database connection failedINFO retry succeededERROR request timed outStep 2: Filter and Count Matching Lines
Pipe 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❯ View Expected Console Output
2ERROR database connection failedERROR request timed outStep 3: Save Output Instead of Printing It
Write 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❯ View Expected Console Output
3 /home/sam/linux-basics-lab/errors.txtWARN retrying requestStep 4: Separate Standard Output and Errors
Capture 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❯ 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
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❯ View Expected Console Output
ERROR database connection failedERROR request timed outPipeline exit status: 0