yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Terminal Lesson 13


5m read
·Nov 3, 2024

That gets what I want here today. This is terminal lesson 13. Um, let's get started. Today's topic is going to be on piping.

Now, this is a little more complex. Once again, if you don't really want to learn anything, you just want to look cool in front of your friends or whatever, you don't have to watch this. This is just for your sake and mine. This is something piping and standard and put standard output.

So first of all, let's get started with every program or, you know, UNIX application. Every command basically has standard input and standard output. What standard output is, like in this case, is the terminal. Standard output is whatever gets printed out. So like if you type passwd, the thing that says “changing password for alex” gets printed out in standard output. “Old password” gets printed out in standard output.

Now, there are a few other things. Say you type a random command, hit enter. Now, there's actually the program, it's called the shell. And what this is, is the command line. Like when you open a terminal, what's running is the shell. And so this little prompt, it's getting printed out in the shell.

Now right here, we see this error that says “bash: command not found.” That's putting out through standard error. And then standard input is basically the way it's getting input. In this case, from the shell, it's from the keyboard, getting input from the keyboard whenever I type.

So, um, that's pretty simple. Anyway, there are a few other things like, um, whenever, like, you might have seen this ps aux vertical bar, you know, like grep something or whatever. grep is actually a program called grep. Like you can type it in right here and nothing will happen. Like this, nothing will come out. You type Ctrl-C.

Now, the thing is grep is just a program. They put standard input into grep, and then it takes a line. If the line contains what you've grepped, then it will print it back out through standard output. So now when you do ps aux, what you're doing is you're setting the standard input of grep as the standard output of ps aux.

So instead of printing everything that comes out here onto the console, into the terminal, it sends it right to grep. And grep then gets standard input and decides, “If this line contains what I'm grepping, I'll print it back out.” And if it doesn't, I won't print it out, and no one will ever see it.

So let's just get this clear. Normally, when you type a command, it sets the standard input to the console. So if I do grep hi then I type ‘crap,’ nothing will come out. But if I take crap hi more crap, then it'll print back out because that line contains hi. But if I type something that doesn't have hi, it won't print back out.

So basically, instead of me typing to grep, ps aux, instead of typing this to the console, it types it to grep. So that's what the vertical bar is. That's what the bar up and down is. It's really a pipe, and I guess I'll be calling it a pipe for now on.

So, um, another cool thing you can do, I haven't showed you up until now, if I just go to my desktop. If you type echo space hi, and you know what echo does. You just do echo hi, and it prints it to standard output. So print, I write out here to the terminal, right? Well, the little issue with that is, well, there's no issue with that. But what the cool thing with that is, is that you can type echo hi space > and that's actually the greater than symbol.

Now I've just printed out hi, but instead of the standard output of echo being into the console, it's to this file called hi that I made on my desktop. Open it up, and there is when I echoed, which is hi. So that's pretty cool.

So I've just basically piped echo into a file. Now I can also pipe it to ~/desktop/ty. That'll just rewrite desktop. Now whenever you do that, you're rewriting a file. So I really suggest you don't do that to a huge file just to add another line to it. If you want to append to a file, you need to >> instead of >.

So it should look something like this: echo hi >> yourfile. So now I've just added hi onto hi. Now hi has two is, you can see right here. So that's actually very useful in long terms because we just type, we can type to a file.

So you could do this with anything. You could do ps aux > ps, now instead of printing everything in ps aux to the console, to the terminal, it's printing it to my file ps right here. This is everything that would show up in a ps aux streaming right here to this file. How useful is this?

So you can do that for any command. Like if you do passwd, and this is gonna be annoying, > nothing will print out except “old password.” I think is standard error, apparently, but standard output went to passwd and it's empty. So passwd does some weird freaky things.

But say you run, let's see, something that prints something out back at you. Let's do something awful. Let's type nano ha. Nothing is going to print out. Nothing is willing to be visible. Everything that's trying to print out is going right to this file. Then I'll just press Ctrl-X, I'll guess and hit enter.

Now you can tell it just saved. Let's see exactly what happened to my file paths. This is everything that normally we'd be printing out. Didn't really get now. Hopefully append be a little different. Now I've written yes, so it has everything that got printed out at any time in nano. And that doesn't matter very much.

It's just really cool you can pipe any program's standard output to a file. You can also pipe standard error. Let's say I do find / -name hi -print. You didn't get a few errors here. Wouldn't it be great just to be able to get rid of the errors? You can! You can pipe just the errors to a file.

We do 2> errors. 2> stands for error. So 2> errors will print all the errors to a file. You can see already a few errors have been printed to this file. Open it up at any time and see any new errors.

So we don't have to get them printed on the screen. So that is, um, piping. I'm sure you have questions; if you do, just leave them in the comment box, and I will answer them. Thank you and subscribe to Mac Heads on the Left. Bye.

More Articles

View All
This Community in the Philippines Converts Plastic Fishing Nets to Carpet | National Geographic
When we first came here, the fish were so depleted because the fishing methods have become more destructive. You could go along a coral reef for ten minutes and hardly see a fish. As a marine biologist, I quickly realized that I really wanted to do someth…
The Ultimate Conspiracy Debunker
The Internet is like a breeding ground for conspiracy theories. While some are just stupid and funny, others promote ignorance and an unhealthy distrust. So we went to the Kurzgesagt lab and developed a foolproof system to destroy not all but a lot of con…
YC Alumni Lightning Round
All right, guys. We uh, we got a break coming up but just a few words in closing, okay? Before we hear from some amazing alumni and then head to our um, happy hour on the roof. Today, we were lucky enough to hear from some of the very best VCs in the val…
How Jeremy Financial Education Makes $50,000 PER MONTH
What’s up you guys, it’s Graham here. So I’m here with a special guest, Jeremy from Financial Education. If you haven’t seen his channel, I’ll put the link in the description. One of the things I always like making with these videos is Jeremy said that I…
It’s Over: Why The Middle Class Is Financially Screwed
What’s up guys, it’s Graham here. So usually when I see a mistake or something that shouldn’t be happening, I like to call it out and share my thoughts because investing is something I take extremely seriously, and today is no exception. During a time whe…
How To Get Rich According To Ray Dalio
There are a million ways to make a million dollars, and in this video, we’re looking at one of them. Ray Dalio, the founder of Bridgewater Associates, the largest hedge fund in the world, is a role model for the world of finance. With a net worth of over …