Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we will learn how to read the contents of a file. Now you may be thinking, why not to use Nano to open and read the contents of a file? Well, you certainly can. In fact I do it all the time. But what we are interested in here are tools which are designed just for reading files. This difference will become important later because we'll learn that to read a file really just means to output the text to your screen and we'll learn to direct that output that places besides our screen and even to other commands. A tool like nano which depends on user interaction would do too much for us and it would get in the way.
Now, we have a couple of options for reading files. The first is cat. cat is the simplest and the one that you would use most often. It has kind of a funny name, because cat is short for concatenate. You see, cat doesn't just read a file. It will concatenate or join several files together. You can think of it as read and output the first file, then immediately read and output the second file, and then the next file and so on. That's its classic and intended use. But if we give cat only one file as an argument, well then it just reads that one file and quits.
There's no second file to ever go on to. So it has a bit of a quirky name, because its name implies using more than one file, while we probably are going to use cat more often on just single files. But we can't use it for both cases. But there's one big problem with using cat when we want to review a long file on the screen. With cat all the output is red and displayed at once. That leaves you scrolling up through the output to find the start of your very long file and then scrolling down again as you read. So, for this case when we have a long file, we have another command that's better called more.
more gives you paginated output. It outputs one page of text and then waits for you to hit the spacebar to move to the next page. Much easier but more also had a problem. more doesn't allow you to go backwards to the pages. You could move through the pages forward, but if you want it to go back, well, then you had to exit out of it and start again from the beginning. That wasn't very convenient, especially if you just wanted to go back one page for just a quick second before you moved on again. So as an improvement on more, we have less. Now, obviously the name less is a play off of more and less offers the ability to scroll backwards.
That also has a little bit memory use. Where more would load the entire document memory, less will just load one page into memory at a time. It's a bit of a Unix joke that since less was an improvement on more, that you can say less is greater than more. Now, on the Mac you can actually say that less is more, because more has been completely replaced by less. You can still type more, but the command that actually will execute is less. The man pages use less. So we have already seen them in action and we have gotten a bit familiar with it. We can use F to go forward, B to go backward, and Q to quit and return to the command prompt.
Let's try them out. So in Terminal, I am in my user directory, and I'll just show you that I have got a couple of new files in there. The first one is called lorem_ipsum.txt and with that is a very long file filled with vague Latin text. Then I also have this file short_file.txt, which is also including the exercise files but it's a very simple file. You couldn't come up with any short file. I just wanted to have something to contrast against the very long lorem_ipsum. So let's start trying these out. So I've got cat and then I can say cat short_file.txt. There you see it.
It just output the contents of the file. Now, as I said, what cat is really for is for concatenating together. So if we had cat short_file.txt and then space followed by newfile.txt. That's just a simple one line file we created in the last movie. There they are. It puts those together. So we've got now the short_file followed by the newfile, one immediately after the other. So that's what concatenate is really designed for. Even though most of the time, you'll probably use it in the first usage just with a single file name. So let's try.
I am going to clear the screen using Command+K. Just cat that lorem_ipsum. Here we go. Ready? Hit Return. Boom! It output all of this text and now look at this scrolled back. Right, I would have to scroll all the way up here and find what I was looking for, not really that useful. So instead what you would want to use is more or less. So on the Mac less is more, so we don't need to bother playing with more at all. We'll just go straight to less and let's try out with lorem_ipsum again and I am going to clear the screen just so we have that old stuff out of there again. And there we are.
Now look at the bottom. It's waiting. It shows me just one page. I am going to hit Space. Now I get the next page. Hit space again. I get the next page. And as I said, F and B allow us to go forward and backwards. So we can toggle between those and then when we are finally done we can hit Q to get out of it. There's one other really useful command in there I want to show you, which is that you can G to go to the beginning or the end. G by itself goes to start of the document. Uppercase G goes to the end of the document, so Shift+G. So let's try it. Let's do Shift+G and we jump to the end.
You could see it says END down there, and I do just a regular g, lowercase g. It pops you back up to the top of the document. So that's a good and useful to know. There are also a couple of nice options that you can add in. You can do less with a capital M for lorem_ ipsum and it gives you a little bit better prompt. So now you can see at the bottom it actually tells me which lines I'm viewing and how far I am through the document. I get a percentage. There is another one. Let's go back here and do n. That shows line numbers. So it numbers all those lines for me and there's lots more options that you can use with less. The man pages for it are very long.
So you can definitely go in there and see all the different things that you can do with it, but that's will get you started with it.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97424 Viewers
61 Video lessons · 84654 Viewers
71 Video lessons · 68860 Viewers
56 Video lessons · 101318 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.