DevOps
Shell Scripting
Wildcards & Redirection

Wildcards & Redirection

Mastering data flow and pattern matching is what separates a beginner from a professional shell scripter.

1. Wildcards (Globs)

Wildcards are special characters used to perform pattern matching on filenames and paths.

WildcardMatch TypeExample
*Any number of characters (zero or more).ls *.log (All log files)
?Exactly one character.ls file?.txt (Match file1.txt, fileA.txt)
[]Any character within the brackets.ls [0-9]*.txt (Files starting with a digit)

2. I/O Redirection

Every command in Linux has three default streams:

  1. stdin (0): Input.
  2. stdout (1): Normal output.
  3. stderr (2): Error output.

Overwrite vs. Append

  • >: Redirects output to a file, overwriting its current content.
  • >>: Redirects output to a file, appending it to the end.
# Overwrite
echo "Start of log" > app.log
 
# Append
echo "New entry" >> app.log

Input Redirection

  • <: Takes input from a file instead of the keyboard.
# Count lines in a file
wc -l < data.csv

3. The Pipe (|)

The pipe is the most powerful tool in the shell. It feeds the stdout of one command into the stdin of another.

# Find a file, filter results, and count them
ls /etc | grep "conf" | wc -l

[!IMPORTANT] Redirecting Errors If you want to capture both normal output and errors into the same file, use: command > output.log 2>&1. This tells the system to send stream 2 (stderr) to the same place as stream 1 (stdout).