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
NASA Spacecraft Is About to Enter Jupiter’s Orbit | National Geographic
The scariest thing to me about Juno are the unknowns. So much about the environment that we’ll have to withstand is unknown. Nothing’s really certain about what’s going to happen. It’s a monster. It’s unforgiving. It’s relentless. It’s spinning around so…
If You Have These 7 Traits, You’re in Your LAST Life Cycle
Narrator: Have you ever felt out of place, like you’re here but not of here? You laugh, you love, you play the part, but deep down something feels off. You watch the world rush by—careers, relationships, the endless chase—but it all feels hollow, like a g…
Graphing negative number addition and subtraction expressions | 7th grade | Khan Academy
In this video, we’re going to add and subtract negative numbers on a number line. The important thing to realize is if you are adding a positive number, you start at some point on the number line and you move that many units to the right. If you are addin…
The One Question That Will Get You Ahead
Let’s have an honest talk, shall we? Do you truly want to be successful? Do you want to live that unbothered life and never worry about anything ever again? If this is your goal, well, it’s time for a reality check. Ask yourself the following question: W…
TRACTOR PULLS: It's Not What You Think - Smarter Every Day 276
This is an absolutely preposterous tractor and it’s pulling [Music]. Something; all these people are in these stands to watch what’s called a tractor pull because it’s awesome. The winner is whoever pulls this sled the farthest before getting bogged down …
Building a Raft | Primal Survivor
It’s easier to carry my raft in pieces and assemble it at the water’s edge. I got this long straight piece, and I’ll use this as my cross beams. I sharpen small pieces of hardwood into nails and use them to hold cross beams in place. I want to make sure t…