2.3 Pipes, Redirections, and Filters
Redirection
Section titled “Redirection”Control where output goes.
Standard Streams
Section titled “Standard Streams”stdin(0): Input.stdout(1): Output.stderr(2): Errors.
Operators
Section titled “Operators”>: Overwrite file with output.>>: Append output to end of file.2>: Redirect errors only.
Pipes (|)
Section titled “Pipes (|)”Take the stdout of the command on the left and pass it as stdin to the command on the right.
Filters
Section titled “Filters”Commands designed to process text streams.
| Command | Function | Example |
|---|---|---|
grep | Search for patterns | `cat log.txt |
| `ls | grep` | List specific files |
less | Pager (scrollable view) | `cat hugefile.txt |
head | First 10 lines | head -n 5 file.txt |
tail | Last 10 lines | tail -f /var/log/syslog (Follow live!) |
sort | Sort lines | sort names.txt |
uniq | Remove adjacent duplicates | `sort names.txt |
wc | Word Count | wc -l (Count lines) |
Real World Example
Section titled “Real World Example”Count how many running processes are owned by “root”:
Text Processing Tools
Section titled “Text Processing Tools”Commands to protect, sort, merge, and transform text.
| Command | Function | Example |
|---|---|---|
cut | Remove sections from each line of files | cut -d: -f1 /etc/passwd (Get usernames) |
tr | Translate or delete characters | `echo “hello” |
sort | Sort lines of text files | sort file.txt |
uniq | Report or omit repeated lines | `sort file.txt |
wc | Print newline, word, and byte counts | wc -l file.txt |
Stream Editors (sed)
Section titled “Stream Editors (sed)”sed is a stream editor for filtering and transforming text.
-
Substitute (Search & Replace):
-
Delete Lines: