Skip to content

← Week 3 - Pipes and text tools

Week 3 - Pipes and text tools: notes

Status: started

Chapter 6: Redirection

I/O redirection is a powerful feature of the shell that allows you to control where the input and output of commands go. The basic redirection operators are:

# Concatenate and display files
cat

# Sort lines of text files
sort

# Report or omit repeated lines
uniq

# Print newline, word, and byte counts for each file
wc

# Output the first part of files
head

# Output the last part of files
tail

# Read from standard input and write to standard output and files
tee

stdout (standard output) is the default destination for command output, while stdin (standard input) is the default source for command input.

You can redirect stdout to a file using > or >>, and you can redirect

stderr (standard error) using 2>. You can also combine stdout and stderr using 2>&1.

To redirect standard output to a file, you can use the > operator. For example, ls > file.txt will write the output of the ls command to file.txt, overwriting it if it already exists.

If you want to append to a file instead of overwriting it, you can use the >> operator, like this: ls >> file.txt.

ATTENTION: ls does not send its error messages to standard output. It sends it error messages to standard error.

ATTENTION: ls -l /bin/usr > ls-output.txt /bin/usr does not exist, in this case because of the > redirection operator ls-output.txt will be truncated, the error message gets printed to standard error.

> ls-output.txt will truncate the file or create a new one if it does not exist.

Group Command

{ command1; command2; command3; } > logfile.txt

Standard Error redirect

A program can produce output on any numbered file streams, file descriptors:

0 - standard input (stdin) 1 - standard output (stdout) 2 - standard error (stderr)

If you want to capture error messages, you need to redirect stderr using 2>. For example, ls /nonexistent 2> error.txt will write the error message of file descriptor 2 to error.txt.

Redirecting both stdout and stderr

You can redirect both stdout and stderr to the same file using 2>&1. For example, ls /nonexistent > output.txt 2>&1 will write both the output and error messages to output.txt.

in recent versions of bash, you can also use &> to redirect both stdout and stderr to the same file: ls /nonexistent &> output.txt.

To append both stdout and stderr to the same file, you can use >> with 2>&1: ls /nonexistent >> output.txt 2>&1 or in recent versions of bash, you can also use &>>: ls /nonexistent &>> output.txt.

Disposing Unwanted Output

You can redirect unwanted output to /dev/null, which is a special file (a bit bucket) that discards all data written to it.

For example, ls /nonexistent 2> /dev/null will discard error messages.

Pipelines

Using the pipe operator |, you can connect the output of one command to the input of another. For example, ls | sort will list files and then sort them.

what is the difference between > and |?

The > operator is used for output redirection, which means it takes the output of a command and writes it to a file. For example, ls > file.txt will take the output of the ls command and write it to file.txt, overwriting the file if it already exists.

The | operator, on the other hand, is used for piping, which means it takes the output of one command and uses it as the input for another command. For example, ls | sort will take the output of the ls command and pass it to the sort command, which will then sort the list of files.

Filters

Filters take input, change it in some way, and produce output. Common filters include grep, sed, awk, sort, uniq, cut, and tr.

ls /bin /usr/bin |sort | less

uniq - Report or omit repeated lines

The uniq command is often used in conjunction with sort to remove duplicate lines from a sorted list.

For example, ls /bin /usr/bin | sort | uniq | less will list all files in /bin and /usr/bin, sort them, remove duplicates, and display the result in a paginated view.

wc - Print Line, Word, and Byte Counts

The wc command counts the number of lines, words, and bytes in a file or input. For example, ls /bin /usr/bin | wc will count the number of lines, words, and bytes in the output of the ls command.

wc -l will only display the number of lines. wc -w will only display the number of words. wc -c will only display the number of bytes.

grep - Search for patterns in text

The grep command searches for patterns in text. It can be used to find specific lines in files or input. For example, ls /bin /usr/bin | grep "bash" will search for lines containing "bash" in the output of the ls command.

head / tail - Output the first or last part of files

head and tail are commands that allow you to view the beginning or end of a file, respectively. For example, head -n 10 file.txt will display the first 10 lines of file.txt, while tail -n 10 file.txt will display the last 10 lines.

Exercise 1

Master grep (with -r, -i, -E), sed, awk, sort, uniq, cut, tr, xargs, wc.

Commands, results, what surprised me.

Exercise 2

Analyse an ASP.NET Core log file: top 10 error messages, requests per minute, slowest endpoints.

Commands, results, what surprised me.

Exercise 3

Learn redirection and pipes (>, >>, 2>&1, |, tee) and process substitution.

Commands, results, what surprised me.

Exercise 4

Bandit levels 13-20.

Commands, results, what surprised me.

Deliverable

A file of 15 useful one-liners, each with a one-line explanation.

Write-up.