Lab 06: More Terminal Tourism

Program Output

When running a command, output is typically printed to your terminal.

This is referred to as standard output (and is technically a file). But what if we want to…

  • write the output to a named file?
  • pass the output to another program?

Printing to the Terminal

Recall that cat can be used to print the contents of a file. Suppose we have the following text in world.txt:

world.txt
Hello, World!...
Goodbye, World!

Running cat world.txt gives:

Terminal
~/ $ cat world.txt 
Hello, World!...
Goodbye, World!

Redirecting to a File

To redirect output to some file named FILE, follow the command producing the output with > FILE or >> FILE.

  • > overwrites the contents of the existing file.
  • >> appends to the contents of the existing file (i.e, it starts at the end).
  • Both will create FILE if it doesn’t already exist.

Here, instead of printing the contents of world.txt, it is written to world_copy.txt (effectively making a copy).

Terminal
~/ $ cat world.txt > world_copy.txt 

Redirecting to a File

Now we append the contents to the end of world_copy.txt:

Terminal
~/ $ cat world.txt >> world_copy.txt 

What does world_copy.txt contain now?

world_copy.txt
Hello, World!...
Goodbye, World!
Hello, World!...
Goodbye, World!

Redirecting to a File

The echo command simply prints its input. What does file.txt contain after running these commands?1

Terminal
~/ $ echo "What?" > file.txt
~/ $ echo "Why?" >> file2.txt
~/ $ cat file.txt >> file.txt
~/ $ echo "Really?" > file2.txt
~/ $ cat file2.txt >> file.txt

Answer:

file.txt
What?
What?
Really?

Redirecting from a File

By flipping the > operator, we can redirect the contents of a file to a program. That is, for some COMMAND and FILE, we write:

COMMAND < FILE

Consider the file, fruit.txt, which is out of order.

fruit.txt
banana
cherry
apple

The command sort prints the contents in the correct order.

Terminal
~/ $ sort < fruit.txt  # gets content from file
apple
banana
cherry

Doing Both!

Well what if we want to save this output into another file1? We can use both!

Terminal
~/ $ sort < fruit.txt  > sorted_fruit.txt 
apple
banana
cherry

sorted_fruit.txt now contains:

sorted_fruit.txt
apple
banana
cherry

Redirecting to a Program (Piping)

In some cases, you’d like to take the output of one program and pass it directly to another program (without an intermediate file).

For this, we have the pipe operator (|)1:

COMMAND1 | COMMAND2

It passes the output of the left-hand program (COMMAND1) to the input of the right-hand program (COMMAND2).

A Pipe Example: sort to uniq

The command uniq outputs all of the unique lines in a file.

But there’s a problem. It only compares neighboring lines!

numbers.txt
1
2
3
3
1
2

Let’s try uniq on numbers.txt

Terminal
~/ $ uniq numbers.txt 
1
2
3
1
2

What’s the solution?

A Pipe Example: sort to uniq

We sort first! We can do this with a pipe.

Terminal
~/ $ sort numbers.txt | uniq
1
2
3

Note that we can use many pipes (as with R)1:

Terminal
~/ $ curl https://raw.githubusercontent.com/dominictarr/random-name/master/first-names.txt | sort | head -n 3
Aaren
Aarika
Abagael

>/>> and |? Why not!

Note that you can combine these operators. For example:

Terminal
~/ $ sort numbers.txt | uniq > sorted_numbers.txt

Commands Today

These are the commands to become familiar with today:

  • sort
  • uniq
  • cut
  • paste
  • grep

Reminder: MAN Pages

Instead of me tediously explaining each one, let’s look at some MAN pages together to learn how to read them1.

Recall that to pull up the MAN (manual) pages, we run the following in the terminal,

Terminal
~/ $ man command     # if you're on Linux or MacOS
~/ $ command --help  # if you're on Windows

replacing command with the name of your desired program.