Full Stack for Front-Ends Part 2 Redirection
This course has been updated! We now recommend you take the Full Stack for Front-End Engineers, v3 course.
Transcript from the "Redirection" Lesson
>> Jem Young: All right. So we do all the fun things. We know how to search for things. We know how to search the side file contents. Let's talk about redirection just briefly. Redirection is a thing that you've already done at least probably once today. If you just ran this ps aux commanding greps, you redirect it with a pipe.
[00:00:17] You redirected the standard output into another program and we just ran through it. So, there's a few redirection operators. The ones we use all the time are PIPE, and this to me is one of the coolest things about Unix in general and Linux, is that they have these operators.
[00:00:40] They're so powerful because it's a very simple mindset on how to design systems. You have standard out, you have standard in, you have standard error. So, most commands on Linux do something with this. And it's so simple in its elegance in that there's only three things. Standard out, what are we outputting?
[00:01:02] Standard in, what are we reading from the file? If there's an error, here's what we do with it. That's it. That's all the things you have to handle. So knowing this, how to design systems, you can write commands that just read standard out, standard in, and they're all uniform because they all follow the same pattern.
[00:01:18] Redirection operators, just a way of changing that. So we're saying find. Okay, I have all those files to put out, I can pipe that into grep, I can look through all those files. And then I could just spit that out to the console, I can write it to a file.
[00:01:31] That's the power of redirection operators. I know, as front-end developers, you're probably like, yeah, that's pretty cool. Knowing this and knowing what you know now, you can build really complex scripts you can build really complex things that are so powerful, just using standard operators. Because you're reading to a file, you're writing to a file, you're handling errors.
[00:01:52] That's it. So, let's do some quick redirection, but you'll see some a bit later in the course but these are just so you know how to do it. How we write to a file. Knowing this, we just point to it, right, send it out to a file. So, say ps aux foo, go ahead and give that a shot if you want.
[00:02:13] You can just follow along at home.
>> Jem Young: Nothing here, nothing on my sleeve. And let's say ps aux and I'm gonna write to a file called foo. What's that do? We can cat foo which exists now and all we did was write the output into this file. You say, that's not that useful.
[00:02:37] Well it is, because with finding grep we can actually do file filtering. We can find all directories, let's just do it right now. What was that fancy thing we did with the logs, yeah. Let's find all the logs and lets write this to foolog. And for cat foolog.
[00:03:00] There, we just wrote all these things to this file. And if you think, that's trivial, when am I ever use this, what do we do most of the time? We're writing to a file or we're reading to a file. Writing to this, reading from this. That's not the say computer in a nutshell, but at its core, that's what we do.
[00:03:16] So if you know how to master these things, you can chain a whole bunch of commands together and I'll talk about it a bit more later. How to get more information on redirection and shells. Because it gets good. So can anybody tell me what this does? I'll go back.
[00:03:35] We'd learned what these operators do, read from standard in, write to standard out. What does this do?
>> Jem Young: Anyway, take a guess, there are no wrong answers. Wrong. [LAUGH] Yeah.
>> Speaker 2: Reads from foo in to bar then-
>> Jem Young: Other way around. You have it. It reads from bar and it writes, it reads from bar, puts it in the foo, and then it spits it out into baz.
[00:04:04] That's pretty cool. So essentially, let's say you have a configuration file and you have a script that reads that configuration file. And you want to output the logs of foo. foo just reads configurations and it takes it from bar. That's how you do it. It looks weird, the syntax kinda just blows your mind a little bit, but that's really powerful.
[00:04:24] That's really cool. Yes.
>> Speaker 3: Is is the same as bar, two bars and then two foo?
>> Jem Young: Bar to foo.
>> Speaker 3: So bar greater than bar and then greater than foo. Is it the same as this one?
>> Jem Young: Not necessarily, because if you do it the other way around.
[00:04:43] So, what you're asking is correct the wrong, this to foo, then type that to baz that. Not necessarily the same way. Wait, which
>> Speaker 3: Can I see this slide?
>> Jem Young: Yeah.
>> Speaker 3: Foo would be the last one.
>> Jem Young: Foo would be the last one.
>> Speaker 3: Or this one, yeah, that's the same, yeah.
>> Jem Young: So no, this is saying, I'm gonna execute bar. So bar is our configuration file, we'll gonna execute bar. All the output there, we're gonna send it to foo. And then we're gonna write it to baz, it's not the same thing as reading the input from bar.
>> Speaker 3: Yeah, I got it.
>> Jem Young: So if we wanna say, cat bar, maybe. Actually, that would work, too. But this avoids running cat, which is another process you'd have to run. It's pretty small, pretty fast, but in general you try not to run processes you don't have to, especially when the shell will handle it for you.
[00:05:39] But that's an excellent question because yes, they will do the same thing. But tell me this doesn't look cooler. Because you guys it's just like, you show someone this and they don't know what's going on. To you it's simple, but if you've never seen this before, this weird syntax, you'll feel like, whoa, this person is like a computer genius.
[00:05:56] You're like yeah. Yeah, I know. So let's talk a bit about shells and just, I'll jump ahead a little bit because I want to give a shoutout to cuz while we're here. Dah-dah. All right. James. Subsect. He has a course entirely on bash, Vim, regX shells. He goes into so much more detail than I would.
[00:06:22] We're just getting the high level, just the good parts. He's gonna go into the nasty part too. It's a course on Frontend Masters, so if you're here, you get it for free. I recommend checking that out. I bet he answers questions like that, on how to pipe things properly, and he's a super nice guy.
[00:06:37] So I recommend that. Yes.
>> Speaker 4: We did have a question about the difference between the pipe and the lesson, I think. Or, well, you're calling it.
>> Jem Young: The pipe and the lesson?
>> Speaker 4: Well, yes, if you wanna check the chat.
>> Jem Young: I don't understand the difference between pipe and input.
[00:06:55] Yes, so pipe takes standard out from one file and pipes it to the next file. So it reads it. The other way, it's taking inputs from one file, as in it's reading some of the file contents and putting that in. They're two different things, because when a program writes, it writes the standard out or standard error.
[00:07:13] So it's saying, I don't know whether we wanna console log hello world on those scripts, that's gonna be piped to the standard out. Versus reading from a file it just reads the file directly. That make sense, hopefully? Yes. Okay.
>> Speaker 5: It's not evaluated.
>> Jem Young: It's not, excellent, yeah it's not evaluated.
[00:07:32] Yeah it doesn't run that file, versus if you pipe something, it runs the file, pipes it back out. Wish I had a whiteboard. No, that wouldn't work here but I would show standard out, standard in. But if you understand those concepts of has an output, has an input, has an error, that's all you really need to know.
[00:07:47] I'm saying that like trivially but that's all you really need to know about how to run pipe commands and how to run even write shell scripts.