Skip to content

Lesson 7: Working with text

Creating and editing text files is one of the most common and fundamental tasks you'll perform on the command line.

Printing text on the screen

The simplest way of printing arbitrary text on the terminal is with the echo command:

> echo "Everything between the quotes followed by the echo command is printed to the screen."
Everything between the quotes followed by the echo command is printed to the screen.

Of course, simply printing text like this on the screen is not particularly useful or interesting. What we are interested in is combining this command with others to do cool and useful things later on.

Writing text to files

As promised, let's move on to something more useful--writing something and saving it as a text file. First, continuing with the Harry Potter theme, let's create a sandbox for our adventures:

> mkdir ~/Hogwarts/Wizards
> cd ~/Hogwarts/Wizards

By default, most commands print their output on the terminal itself. In nerd speak, this is known as printing to standard output (often shortened to stdout). As an example, running ls prints the list of files to standard output.

Knowing this, let's use a neat trick to redirect the output of an echo command to a file instead of standard output:

> echo "It's levi-OH-sa, not levioSA." > Hermione.txt
> echo "You're a little scary sometimes, you know that? Brilliant, but scary." > Ron.txt

Here, the > symbol told the system to redirect the output of the echo command to a file instead of the usual standard output (your screen). This is called output redirection.

Note: The > symbol used here does not have anything to do with the same symbol I use at the beginning of each command. The former redirects the output elsewhere instead of the standard output, while the latter just a prompt--you have to type the command after the symbol into the terminal.

Reading the file we created

The simplest way to read a text file is via the cat command. Despite what the name suggests, cat stands for concatenate, and does not have anything to do with felines. Run cat followed by the name of the text file that you want to read:

> cat Hermione.txt
It's levi-OH-sa, not levioSA.
> cat Ron.txt
You're a little scary sometimes, you know that? Brilliant, but scary.

As you'd expect, you can use the cat command to view the contents of any plain text file.

Adding text to an existing file

One thing to be keep in mind is that running the echo command on the same file with more text will overwrite the file contents instead of adding to it:

> echo "Books! And cleverness! There are more important things—friendship and bravery." > Hermione.txt
> cat Hermione.txt
Books! And cleverness! There are more important things—friendship and bravery.

To add (or, append, in geek speak) text to an existing file, use the >> symbol instead of >:

> echo "When in doubt, go to the library." >> Ron.txt
> cat Ron.txt
You're a little scary sometimes, you know that? Brilliant, but scary.
When in doubt, go to the library.

There is much more you can do with text on the terminal. In fact, entire books have been written on the subject. For the sake of keeping things simple and digestible, I won't cover all of that in this course, although some more essential text manipulation tricks will come in later chapters.

Exercises

  1. Practice writing, reading and appending text to more text files, including any existing text files in your Home folder.
  2. Instead of just one file, run cat with multiple arguments, such as cat Hermione.txt Ron.txt ~/someothertext.txt. Is the output similar to what you've seen with multiple arguments with other commands such as ls?
  3. Try text redirection using > and/or >> with other commands such as ls, pwd and mkdir. For example, run ls > list.txt.
  4. Create a new file using >> instead of >, for example, echo "I don't go looking for trouble. Trouble usually finds me." >> Harry.txt and read it with cat.

Doubts and explanations

Running cat with multiple arguments

Similar to running something like ls Pictures Documents Hogwarts, cat displays the output of all the text files one after the other:

> cat Hermione.txt Ron.txt ~/someothertext.txt
Books! And cleverness! There are more important things—friendship and bravery.
You're a little scary sometimes, you know that? Brilliant, but scary.
When in doubt, go to the library.
Text from the third file.

Text/output redirection with other commands

You can redirect the output of almost any command that writes text to a file. Do note, however, that > or >> alone do not write the contents of error messages to files. There are special symbols for that -- you'll learn about them in later chapters.

But wait, I tried mkdir newfolder > mkdiroutput.txt, but the file came out empty!

Since the mkdir command does not print any text or confirmation messages when it's successful, there is no output to redirect, hence the blank file. As mentioned above, the > and >> symbols do not write error messages to files, running mkdir > existingfolder won't work, either.

Creating new files with >> instead of >

The >> symbol appends text to a file. Since the file Harry.txt did not exist, it adds the text to a blank canvas, which ends up creating a new file.