Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Unix for Mac OS X Users unlocks the powerful capabilities of Unix that underlie Mac OS X, teaching how to use command-line syntax to perform common tasks such as file management, data entry, and text manipulation. The course teaches Unix from the ground up, starting with the basics of the command line and graduating to powerful, advanced tools like grep, sed, and xargs. The course shows how to enter commands in Terminal to create, move, copy, and delete files and folders; change file ownership and permissions; view and stop command and application processes; find and edit data within files; and use command-line shortcuts to speed up workflow. Exercise files accompany the course.
Up until now we've been seeing how to direct input and output, but we've been doing it to files and from files. So we have to have these files as sort of the intermediary step between them. What would be great as we could actually take the output from a command and use that as the input into another command, without having to go to a file in between. It's something that you are going to use all the time in Unix. In fact you'll probably use it more often that you will either of those file redirects. To do it, we use something that we called the pipe. I'm just going to type one just so you see what it looks like.
We hold down the Shift key and then the Backslash. That's the key that is above the Return key. You get this upright pipe. So that's what I refer to whenever you hear me say pipe and typically we say we pipe something into something, right? We use it as a verb as well. So if we have something like echo "Hello World", we know that would normally output to our Terminal, but if instead we use the pipe, we can redirect that into another Unix command. Don't use a file here. A file is used with those other operators.
Here we can use another command. Let's just do wc as a simple one. So Word Count comes up and it reports to me there is one line, two words, 12 characters. You see what it did? So it took the output from one command, piped it into the other one, so that it received it as input and then that's what it uses. Let's take another example, let's say echo, let's put in our mathematical expression we used before, (3*4) + (11*37), and let's pipe that into bc, right. Before, remember I saved it to a file and then I took that file as input to bc.
Now I'm just saying all right, take this directly and put it into bc, right. It gives me back a result. I piped this output from echo into bc. If we take our fruit file, remember I'm inside my unix_files directory, right here, ls -la. I have this file fruit.txt that we've worked with a few times. Let's cat that fruit file. It's just a list of fruit in an unsorted order and let's pipe that into sort. Notice before I was taking the file and sending it to sort. Now I'm actually outputting it, right. This is generating output and that output is what's being sit in their, okay. Ot's a subtle difference, but it is different and you see we get back this result.
Let's not take that and let's pipe that through unique. So now I get the unique version. Now we've seen a couple of different ways that we can do this, that we can sort them and make them unique. Sort has an option that does it. We can take it from sort into a file and then a file back into unique. I wanted you to see what we're doing here. We're piping it along from one command to another, so we get a string of commands. The first one is catting the file, then we're sorting it, then we're doing unique and at the end of all that, we could actually output it. Let's say unique_sorted_fruit.txt. I already have this file, so it's going to overwrite it. There it goes.
So now all of that went through all those commands and at the end of it then I directed the output to a file. See how that works? It's really, really useful. You could also do something similar to what we did before where we could say well, sort should get its input from fruit.txt, all right. We know what that's going to do, right. That does that and then we could take that and we could pipe that into unique. We know what that does, right, and then we could take that and we can send that out to unique_sorted_fruit, but that's a little less readable. If you just compare the two, I think it's a lot easier to understand what's happening here.
We're reading the file, sending it to the sort, sending it to unique, then it ends up in the file. The other one, you have to jump back and forth to read a little bit. So, both of them accomplish the exact same thing. I just think that the second one is perhaps a little more readable. Another place where this is really useful is we have a long file like our lorem-ipsum file, right. It's very long. We can pipe it into less. There we go. Now we are in less. We can go page by page. Now of course that's the same thing. We could do less lorem-ipsum.txt and that gives us the same results. This takes a file as its argument, but for something like ps aux, now that doesn't take a file as an argument.
That's the processor output that we saw. So we are seeing a list of all our processes piped through less. Guess what? Now we get pagination. We can go through them page by page and we can use forward and back to look at those. All right, there is a very common usage of it is to take something that we're working with and pipe it into less. We will be using this type a lot especially when we get to the chapter on the Unix Power Tools where we look at a lot of the advanced features. We will be able to use this pipe to great effect. So the main thing to remember with pipe though is that unlike the other operators, we're piping it into a command, not into a file, into a command and the output from it should be the output from a command, not from a file.
Even here this is actually output of a command, not a file.
Find answers to the most frequently asked questions about Unix for Mac OS X Users.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.