Look inside a file

INPUT · Slides

Put what is in a file on the screen

01 / 08

ls only tells you the name

You can use ls now, from the last chapter. But what ls shows you is only the name.

You can see that there is something called memo.txt, but not what is written in it. Looking inside takes another tool.

This chapter is about tools that check what is inside without opening it. The first is cat.

~ $ lsgreeting.txt  memo.txt  names.txt  recipe

02 / 08

cat — the whole thing, on the screen

Write a file name after cat and it puts what is inside straight on the screen.

Open it, read it, close it — three moves become one line. You cannot edit anything, but if you only want a look, this is the fastest thing there is.

~ $ cat greeting.txtgood morning

03 / 08

The name comes from "join"

cat is not the animal. It is short for concatenate (join together).

It was built to "join several files into one", and putting them on the screen was a side effect. Give it only one file and there is nothing to join, so it just comes out as it is.

Which means give it two and they come out one after the other.

~ $ cat greeting.txt names.txtgood morningSmithJonesBrown

04 / 08

-n numbers the lines

Add -n and each line gets a number in front of it. First letter of number.

Useful when somebody says "the error is on line 12" and you would otherwise be counting.

Options are the same shape as ls -l and ls -a in the last chapter. A - and a letter after the command is a pattern you will keep meeting.

~ $ cat -n names.txt     1	Smith     2	Jones     3	Brown

05 / 08

Do not use it on a big file

cat has one weak spot.

Even with tens of thousands of lines in there, it tries to put out all of them. It will not stop halfway. Point it at something like a log file and the screen pours past like a waterfall, leaving you with the last few lines.

That is what head (just the top) and less (a page at a time) are for. They come later in this chapter.

cat is for short files, and that is worth remembering.

06 / 08

Write to a file with >

cat does not only put things on the screen — you can also make a new file with it.

> is the symbol for "point the way out at a file". Write cat a.txt b.txt > c.txt and the joined result goes into c.txt instead of onto the screen.

This > gets a proper look in the chapter on joining commands, so for now it is enough to know the symbol exists.

~ $ cat greeting.txt names.txt > all.txt~ $ cat all.txtgood morningSmithJonesBrown

07 / 08

Point at a file that is not there

cat a file that does not exist and you get this.

It is the same message you got when you mistyped a cd in the last chapter. No such file or directory.

Linux does not have many kinds of error, and the same ones keep coming back. So learn to read one and you can read it everywhere else.

~ $ cat missing.txtcat: missing.txt: No such file or directory

08 / 08

Now have a go

From here you type into the terminal.

Marking looks at what came out on the screen, so cat ./memo.txt counts too. Same as the last chapter — there is no one way you have to write it.