Check out a free preview of the full Complete Intro to Linux and the Command-Line course
The "Reading Files" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:
Brian gives an overview of the commands on the CLI that allow developers to read a file, to create one or multiple files, to learn more about a specific program, or to view a snapshot of a given file.
Transcript from the "Reading Files" Lesson
[00:00:00]
>> The next section we're gonna go into is files, pipes, and permissions here and we're gonna go with interacting with files. And so we're gonna get more into how to read files, interact with files, those kinds of things. And so this section in particular, and what you're gonna see for this is I'm gonna go over just a lot of programs that you need to know or commands or whatever you want to think of them as.
[00:00:19]
Okay, so let's just go ahead and hop back into it. So open your command line here, which I got. So one of them I wanna introduce you to right away is one called less. And less allows you to read a file, so it's kind of like vim that you're opening it into a full page like reader mode, but it's just for reading.
[00:00:46]
It's not for writing or anything like this or anything else. So if I say less textfile.txt. Notice it throws me into this file and I'm in a whole program now and it actually even kind of looks a little bit like vim. But this allows me to read a file and It's really good for long files.
[00:01:07]
And then when I'm done with it, it's the same thing as vim, I just hit Q, and it pops me back out of it. So, less is one that's really useful to know. Let's just go in here to the text file real quick and I'm just gonna make it super long.
[00:01:23]
So if I say insert and I'm just gonna add a bunch of new lines here. Okay, so now if I look at less txtfile.txt. Now if I hit down and up, you can see it's scrolling up and down. That's what less is really good for, less is really good for long files.
[00:01:50]
And there's special shortcuts for navigating quickly that I don't know because I don't use less enough to learn all the shortcuts. And that's another kind of thing that you'll find with these bash commands is you'll invest time into the tools that you use a lot. I know git very, very well cuz I use git probably the most on the command line, but do I know less very well, no, I don't.
[00:02:15]
Yeah, so there's like a way you can like search, I think if you say like, slash then type hey. Yeah, so you can like search for things, I remember that you can use slash, but there's a bunch of things like that less can do for you. And yeah, cool, so, a question that a lot of people probably have is what about more, right?
[00:02:42]
Cuz we're talking about less, but what about more? So if I say more textfile.txt, pretty similar, right? You kind of get popped into this same sort of thing. When should you use less and when should you use more, just use less. [LAUGH] So more is the older version of our older version.
[00:03:02]
It is an older program, that someone down the line got sick of the things that less or that more couldn't do, so they wrote less. And again, being a snarky computer scientist, they called it the opposite of more. So that's where the name less comes from, it's the newer version of more.
[00:03:20]
More is just included because it's been there historically, which is again, a very common pattern for which ones are included. And less just has more features, it's just a general better thing to learn, so I would say just learn less. Yeah, less is a better thing to learn, and there's other things that are even newer than less, there's most, right, but most isn't installed by default.
[00:03:50]
And I tend to just learn the ones that are installed by default. Cool, so that's less, very useful for reading long documents or documents of any variety really. Yep, it's meant to mimic vim to some degree, the same keys. All right, so let's talk about man, M-A-N, so man stands for manual.
[00:04:15]
If you want to get in depth about a program, it'll give you the manual basically for those programs. So if I say man less, it'll give me the manual for less. Right, so if I scroll down here, it'll give me like a very in depth with here's everything you could ever want to know about less.
[00:04:41]
And you can see it's pretty long. You can see down at the bottom, it's about 1000 lines long. That's how long it is. Now, the reason why I showed you less before I showed you man is that man uses less. So if you learn how to use less, you already know how to use man.
[00:05:06]
The nice thing about it is you can also do less -- help and this will give you the abbreviated version, right? You can see here it's the summary of less commands, right? But man will give you the entire manual. Now, you might ask me, how often do I actually end up using man?
[00:05:24]
Not very much because it's usually too much information, it's usually more information that I want. And I'm usually better off just googling for whatever thing I'm looking for if I'm getting into the man pages. It is useful for whatever reason, like if you don't have an internet connection and you wanna look at how to use a program, man can be useful.
[00:05:45]
But also not everything has a man page as well, so keep that in mind as well. I don't know if Python 3 has a man page, it does. So it does, so some of them don't have fan pages. Yeah, if you want the exhaustive thing, use man. If you want just the abbreviated version, use -- help.
[00:06:17]
All right, let's pop into cat, it's one that I end up using a lot. So, if I say cat textfile.txt, it just prints everything out and it doesn't do it in less for like less. Gives you like a nice scrolling, makes it nice to read. Cat just grabs the entire file and spits it out for you, so you can see here I said cat textfile.
[00:06:44]
This is Brian, blah blah blah, and also has all those empty lines there in the middle. So, if you have a really short text file, it's pretty useful to use cat instead of less. We'll use cat more later. Because what it's actually doing is it's actually adding that file to the standard out.
[00:07:04]
And you can use that to be interesting. But if you don't know standard out is yet, we'll talk about that in a little bit. You might ask why it's called cat. It's from concatenate, so it's concatenating your file to the standard out. That's why it's called cat. Okay, we already saw tail a little bit.
[00:07:29]
So if I say tail textfile.txt, notice it just outputs the last ten lines. That can be really useful for various different reasons. If you wanna just look at the logs but you only wanna select the last ten things from the logs you can say tail textfile.txt. And it's the same thing with head, it'll just do the first ten lines, and you can control how many lines that is.
[00:08:00]
How many is it? It's n, so if I say tail -n 3, textfile.txt, it only gives me three lines. Same thing with head. The same thing if I want 50, it'll give me the first 50 lines. So this can be useful if you're looking for just one particular part of the file.
[00:08:29]
We also showed you the -f which shows you how you see in real time. So this is tail-f textfile.txt. This will show me in real time as things are added to the file, right now nothing is being added to it, but if there were they would show up.
[00:08:49]
This would be useful for like tailing the logs. In fact, you might have even heard that term before tailing the logs, right? It's from this tail. So if you do that, then you can use Ctrl+C to get out of that. In fact, Yeah, I'll show you this, you don't necessarily have to follow along with this, but I'll show you.
[00:09:19]
If I do tail -f textfile and then I'm gonna open another terminal. So I can say here inside of this multipass shell primary. So you can see here I have two terminals running on the same thing. So if I say echo hi and I'm gonna attach that at the end of textfile.txt.
[00:09:48]
If I go into here, you can see it found hi cuz I added new information into using this-- operator. We'll talk about this in a little bit, but all that does is appends new text to the end of a file. There you go, that's what the tail -f is, if you wanna follow in real time, those files having things added to them.
[00:10:25]
Cool, let's get into make directory. So I'm here in my Ubuntu home directory. Let's say I wanted to make a new folder in here, you can say make directory, mkdir, and I'm just gonna call it my-new-folder. And you can see here, I made a new folder. So I can say cd my directory, And you can make more folders as much as you want.
[00:11:02]
So that's when you'll end up using a lot [COUGH]. One that I end up using quite a bit as well is mkdir -p. And then if you give it multiple folders, it'll create all those folders for you. So I can say -p hi/my/name/is/Brian. So now you can see I made a file or a folder called hi.
[00:11:31]
And in here is another my, and you can just, my/name/is/Brian, right? I don't know why you would do that, but it's there. So the -p just allows you to make multiple folders nested all at once.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops