Skip to content

Lesson 8: More practice with text

Building on the previous lesson, let's learn some more essential tricks for text manipulation before moving on to other concepts.

Echo: Write multiple lines with a single command

So far, you've only learnt how to write a single line to a file at any time, which, of course, is very tedious if you want to write multiple lines. Fortunately, writing multiple lines in a single command is rather simple--after writing one line, press Enter/Return instead of closing the quotes, type in the second line, close the quotes, then hit Enter again:

> echo "The famous Harry Potter, can't even go to a bookshop without making the front page.
> Longbottom, if brains were gold, you'd be poorer than Weasley, and that's saying something." > Draco.txt

> cat Draco.txt
The famous Harry Potter, can't even go to a bookshop without making the front page.
Longbottom, if brains were gold, you'd be poorer than Weasley, and that's saying something."

You can write as many lines as you like with this method--keep hitting Enter after every line and close the quotes when you're done.

More on concatenating

Combine multiple files

Another cool thing you can do with the cat command is to combine several files into one. Run cat with the files that you want to combine as arguments, and redirect the output to a different file:

> cat Harry.txt Ron.txt Hermione.txt Malfoy.txt > "Wizard quotes.txt"
> cat "Wizard quotes.txt"
I don't go looking for trouble. Trouble usually finds me.
...
Longbottom, if brains were gold, you'd be poorer than Weasley, and that's saying something.

Overwrite another file

You can overwrite an existing file by redirecting another file's output with the cat command.

> cat Draco.txt > Ron.txt
> echo "This is my file now!
> Love,
> Draco" >> Ron.txt

> cat Ron.txt
...

A better way to read longer files

cat for reading text files only works for short files--5 to 10 lines of text. Anything longer, and you get to see only the bottom part of the text. To see what I'm talking about, right-click and download this long Lorem ipsum text file, open it first with a regular GUI text editor, then with cat on the command line and compare:

> cat ~/Downloads/lorem_ipsum.txt
...
(cue long, almost unreadable wall of text)
...

Fun fact: "Lorem ipsum" is a placeholder text first used by the printing and typesetting industry before a final copy was available. Here, I'm using it to demonstrate how bad long text files look with a cat command.

In theory, you can read such a large file from the top using the extremely awkward ctrl-shift-PageUp and ctrl-shift-PageDown combinations, but it's not ideal.

In such a case, the less program comes in handy. Think of it as a PDF reader, but for plain text files:

> less ~/Downloads/lorem_ipsum.txt

You'll notice that it's a much saner way to read large files--the Up / Down, PgUp / PgDown and Home / End keys work as expected. There is support for pattern search (as usual, case-sensitive), too--press the / key for search mode, followed by the text you want to look for. Move to the next and previous match with the n and N (shift-n) keys, respectively.

To quit the program, press q.

Basic, but works.

Exercises

There aren't a lot of special use cases for the commands in this lesson-- just practice combining, overwriting and reading more text files.