Check out a free preview of the full Complete Intro to Linux and the Command-Line course

The "Output Streams" 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 explains that one of the Linux philosophies is the assumption that the output of a program can be the input to another program through streams and pipes, demonstrates how to redirect the output to another file. This section focuses mainly on 'stdout' and 'stderr'.


Transcript from the "Output Streams" Lesson

>> This is kind of a fundamental concept, and we've been using them without you necessarily understanding that they're being used. So, I eluded to this in the very beginning when I'm talking about the Unix philosophy that you have to assume that the output of any program could be the input to another program.

And the way that this is accomplish lead us to things to kinda like connected together through something called streams in pipes. So, you'll have this one stream of characters and you'll pipe that into another stream of characters. So, a good example of that is, I'm gonna make another folder.

We'll just call this like streams. Let me go into streams. And let's say I have a string of text and I want that to be output to another file. What I can do is I can say echo, this is my text, right? So, if I say that, right?

This just that outputs it to the standard out pipe, right? Or to the stream, the standard out stream. And if you don't have a stream that goes anywhere, it eventually just says I'm just gonna output that to the user, right? So that's kind of the terminus. It's showing that to the user.

But let's say I wanted to put that text into a file. So I could do that echo "this is my text", and then if I put 1 and an angle bracket like that, I'm going to redirect that stream to another place. Into a new file.

So, now notice first of all, it didn't output that to my screen so it never echoed anything. So, now notice first of all, it didn't output that to my screen so it never echoed anything, but now I have this new-file.txt. And if I cat that file, you'll see that my text went into that.

That's pretty cool, right? So, I'm redirecting that output from hitting the screen to going into a file. This is the standard out. So 1 and then angle bracket, redirects just standard out. It doesn't redirect anything else it just redirect standard out. So, what happens if I do cat new-file.txt?

But instead of letting that go to my screen, I redirect that to another-file.txt, what do you think that's gonna happen then? I just copied the file, right? Like I basically, just did copy. So, if I cat another file now, it's this is my text, but I just redirected that from standard out from going to the screen into another-file.

So, it's basically, a poor man's copy, okay? So, if I do this again, I have new-file.txt. But if I put a second angle bracket there, what it does instead of replacing the file, it appends to the file, right? So, now if I look inside of another-file.txt, this is my text shows up in there twice.

Whereas if I did it with just one of these, It's just once now because what I actually did is I said, throw everything out you had before replace it with new stuff. So, two of them adds and one of them replaces. Okay? So that is standard out. Everything that I've shown you so far is you're basically redirecting the output of a file or the output of a program.

And you can do with anything right? So, I can do ls -lsah, and I want to redirect that to ls.txt, right? And now if I go into ls.txt, it's just the output of that. Something that's kind of interesting if I just do cat or sorry if I do ls into ls.txt, so if I do ls right now, it does it this way where it's just like one file per line.

If there's a directory in there, it'll actually colorize that for me, but right now if I cat ls.txt. Notice that there are new lines, and there are no color would ever show up here. That's because a program can detect that it's being redirected. And it can actually, like reformat itself to be more machine friendly, right?

And, so this is a more machine friendly where it's one file per line. Just kind of interesting. So, let's talk about standard error for just a second. So, we've been just been looking at standard out which is like output of a particular program. There's another stream that we're also reading from at the same time called standard error.

So, let's try this again. Let's say, if I say cat non-existent-file.txt, I get, hey, that thing that you asked for doesn't exist. But what happens if I try and redirect the standard out of that to cat.txt? Well, notice that I still see the error message and nothing went to cat.txt.

Why is that? Well, we directed standard out, which is where all the output goes, and there was no output because that was not a real file. It does create the file because it assumes that stuff is gonna end up going there. But we still saw the error output come out here, wherever that was here.

That's because standard out, and standard error are different. Standard error is just for errors and like things like that, so it's actually frequently useful to redirect standard out somewhere. This will be like all of your reporting and metrics and all that kind of stuff, maybe for like your server if you're running a server that way, and then you can redirect standard.

And it airs somewhere else and this will be like your airlocks, right? That's why they kind of separate those two things out. So, if I can do this now. So, instead of being a one here, I can redirect this to be a two which is gonna be standard error.

And then all of a sudden this to error.txt. So, now I have error.txt, and if I cat error.txt, you'll see that the error message actually ended up going there. Makes sense. So, again one angle bracket redirects standard out, two angle bracket redirects standard error. And yeah, so actually that's a good point is you can actually do both at the same time.

So, if I do ls -lsah, like that, and I can do 1> ls.txt and I can do, 2> ls -error.txt. That's frequently all you'll see, is you'll redirect standard out one place to redirect standard error, a different place, and they just go to the kind of separate ways.

Well, labs that I want him to go to the same place. You can actually just leave the number off like that, and that will redirect both standard out and standard error to the same place. So, unless I am doing something where I specifically want to separate the streams I usually just do this, and I'll just redirect everything to one file.

Keep in mind that the if I wanted to append to that, that's how I would do that. You can also do that with two as well, where you can append to the error file, right? So, the multiple error brackets always works. So, let's say I was doing something and I only cared about the standard out and I just didn't care what was happening in the errors whatsoever.

You can do ls -lsah, and I can just redirect the errors to /dev/null. This is a special file here that basically says, I'm gonna throw it in. It's just a black hole of stuff in. Stuff is gonna go into it and like don't save it, don't care about it.

It's just into oblivion. So if I did this, if there was any errors, I wouldn't see them. And by the same token, if I wanna run a program and I don't care about the output, I just want to know if there was any errors. Same thing here, I can redirect standard out to /dev/null and only errors will show up here.

So, let's see if I did here, cat. Some file.txt. I did see the error here and that's how I know an error happened. The same thing here, if I change this too, I'm just not gonna see anything cuz it's gonna ignore all the errors. So that's what dev/null is for, dev/null is specifically for, I don't care about whatever is going into here.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now