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
Mario vs. Link: Who Would Win?? NERD WARS
Nerd wars time! It’s Friday again! We’re gonna do some old-school action today. Oh, nerd wars! Someone suggested this on the YouTube channel comment, you know that thing down here? And I was too lazy to figure out who it was. I forgot, but here it is: Lin…
Neo-Confucianism and Zhu Xi | World History | Khan Academy
In previous videos, we’ve talked about some of the major schools of thought that emerged at the end of the Joe Dynasty, especially as we start to enter the Warring States period. The famous hundred schools of thought, and most prominent amongst them is Co…
The Waters of Slovenia | National Geographic
My connection to the sea started when I was little. I spent most of my summers at the sea, swimming. Ever since I was two and a half years old, I started swimming. I kept on developing a love for the water. The water, here, our skin is different from anyw…
Adding two digit numbers on a number line
We’re told that Cara had a tower with 42 blocks. She added 12 red blocks, 14 more blue blocks, and 16 purple blocks. So, what we want to do in this video is think about how many total blocks Cara now has. To help us with that, we are going to set up a lit…
Fibonnaci on a Marble-Powered Computer
This is the Turing Tumble. It is a marble powered computer. So sorry nerds, it’s kind of a jock thing now. What you are watching is my solution to a puzzle posted on their forums. I have programmed the machine to output marbles according to the Fibonacci…
The $3 Trillion Private Equity Bubble is Finally Bursting
There’s been a lot of talk about how the U.S. real estate market is in a bubble, but people are getting it wrong. The real bubble is in a little corner of the finance industry that is unknown to the average person. This industry has trillions of dollars i…