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
Using Religion As A Tool | Bin Laden’s Hard Drive
MAN: It’s impossible to understand Bin Laden without reference to his religious beliefs. This was a guy who, when he was a teenager, was praying seven times a day, fasting twice a week. On the other hand, he was also a mass murderer. What was his relation…
Letting Someone Go | Taoism for Broken Hearts
Letting Someone Go | Taoism for Heartbreak The philosophy of Taoism revolves around letting go, accepting, yielding, and going with the flow. All Taoist sages seem to agree that it’s better to be detached and indifferent, allow change to happen, and move…
YC Tech Talks: Defi and Scalability with Nemil at Coinbase (S12)
Cool! Thanks everyone. I’m super excited to talk. My name is Nimail. I’m at the head of crypto at Coinbase, and I’m excited to talk to you today about DeFi and scalability. Um, but in part of talking about that, I’ll talk about the landscape for crypto an…
5th roots | Mathematics I | High School Math | Khan Academy
Let’s see if we can calculate the fifth root of 32. So, like always, pause the video and see if you can figure this out on your own. So, let’s just remind ourselves what a fifth root is. So, if x is equal to the fifth root of 32, that’s the same thing as…
Polymerase chain reaction (PCR) | Biomolecules | MCAT | Khan Academy
I’m here with Emily, our biology content fellow, to talk about PCR or polymerase chain reaction, which you’ve actually done a lot of. Why have you done PCR? PCR was kind of the mainstay of my graduate project, where I built all sorts of different recomb…
You Don’t Have To Be A Billionaire To Launch Satellites
Last year, the team at Astranis launched their first commercial satellite into space. Stage separation confirmed. [Music] [Applause] Their smaller, low-cost telecom satellites represent a major breakthrough for the aerospace industry, and they’re also the…