Photo by Ricardo Gomez Angel / Unsplash

Exercise: Redirection Operators

Exercises Oct 1, 2025 (Oct 2, 2025) Loading...

A set of testing-relevant exercises to help testers use, and see the use of, redirection operators on the commandline.

To move information between commandline commands, use | to link inputs to outputs. Use < to take the input from a file, then use > and >> to put the output into a file. More info at Redirection Operators for Testers.

Use environments at https://redir.workroomprds.com

Password is password, please choose a name to go to a home page, from there pick code server, and you should be in vscode.

From there, open your home folder as a file browser, and open the terminal, and get the files needed with git clone https://github.com/workroomprds/for-redirection-operators.git

Exercise 1 – count the unique values in a table

We'll use items.csv

Use cut -d',' -f4 items.csv to see all the country codes.

Use cut -d',' -f4 items.csv | sort | uniq -c to pipe that through sort and through uniq -c . You'll see the count of unique country codes.

Try using an input redirect: cut -d',' -f4 < items.csv | sort | uniq -c .

  • You can move that redirect to the front of the command: < items.csv cut -d',' -f4 items.csv | sort | uniq -c .
  • You can use cat and a | to do (much) the same: cat items.csv | cut -d',' -f4 items.csv | sort | uniq -c .

By now, you'll be frustrated that one of those codes is not a country code? Use tail -n +2 to cut out the top line.

how I did that...

< items.csv | tail -n +2 | cut -d',' -f4 | sort | uniq -c

Let's use sort -nr and head -3 to see just the three countries with the greatest supply.

how I did that...

`< items.csv | tail -n +2 | cut -d',' -f4 | sort | uniq -c | sort -nr | head -3`

Use > to put that output somewhere permanent.

Exercise 2 – split up find's output

find has different outputs. It sends names of files found to stdout, and errors to stderr. Generally, stdout goes to the terminal (as a reply to your command), and so does stderr. If you're looking for one file on a disk, and you've got a heap of permission problems, you'll see mainly permission problems.

Let's use the following, without sudo

find / -name items.*

compare with

find / -name items.* 2>/dev/null

The 2> sends all the permission problems to dev/null, which is unix's bottomless pit, so you won't see permission problems at all.

Use > to send stdout to a file (leaving stderr visible)

Use &2> to send stderr to a file (leaving stdout visible)

Exercise 3 – reading a command

What does this do?

find /var/log -type f -print0 | xargs -0 file --mime-type | cut -d: -f2- | tr -d ' ' | sort | uniq -c | sort -nr

(sorry that it doesn't wrap...)

You can use it and try cutting bits out to see experimentally, or ask an LLM.

I reckon it...

  • lists all the files in /var/log – and lists them safely and with null delimiters
  • uses xargs to split at the nulls
  • passes them to the file command, asking for the type
  • cuts out the filetype column
  • strips whitespace
  • makes a count of type
  • sorts by most common type

Exercise 4 – last 10 lines of recent logs

This uses redirections – and also xargs, which runs code on each line of input

  • list files (with paths), sorted by recentness
  • take the top 3
  • show the most recent 10 line of use those files-with-paths

sudo ls -1td /var/log/* | head -3 | xargs -I {} sh -c ' sudo tail -10 {} '

do this to add a descriptive line between log files

sudo ls -1td /var/log/* | head -3 | xargs -I {} sh -c 'echo "=== {} ==="; sudo tail -10 {}'

Note the double sudo... one for the ls, and a different one for the tail inside the xargs block.


sprue from here...

Exercise 4 – streaming into tail

Exercise 5 – overwriting and appending

Member reactions

Reactions are loading...

Sign in to leave reactions on posts

Tags

Comments

Sign in or become a Workroom Productions member to read and leave comments.

James Lyndsay

Getting better at software testing. Singing in Bulgarian. Staying in. Going out. Listening. Talking. Writing. Making.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.