Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we've seen how to change the standard output to go to a file, I'd like to see how to direct input from a file that is instead of using our standard input which would be our keyboard, the things that we type into the command line, we are going to use input from a file. Notice that I'm inside my User directory and I'm inside unix_files. These files are in the Exercise Files as well. The one I'm going to be using to start with is this fruit file. You will remember what's in there. It's just a list of fruit, all right, a bunch of unordered fruit with duplicates in there as well.
So what I'm going to do is I'm going to use the sort and we saw that we could do fruit.txt. Essentially what this is doing when we call that is providing as argument fruit.txt which the sort function then uses as its standard input. So it's already doing what we're talking about for us, but I want to show you the way that we can do it all the time, sort of universally for any command that takes input, even if the input is just from the keyboard. We can then use this less than sign. That's the opposite of what we used for output. It's facing the other direction, pointing to the left.
What we are saying is take this fruit.txt file, use that as your input, and send it to the sort command. Use it as if we had actually typed those commands in there. And you will see that it gives us back through the results. Now those results are still coming to our screen. We are not redirecting those right now. We are focused just on directing the input. Let's try another one, wc < lorem_ipsum.txt. So there you see. Now it's using the contents of that and passing it into wc to count it. Notice that it doesn't show the file name anymore, which it did before when we called it the other way, right.
When we call it this way, it actually uses that file name. So this is providing the file as an argument. This is providing that file as input that's being passed to this command. It is a subtle difference. Let me show you one way that maybe a little more obvious. Let's say that we echo, a mathematical expression, (3*4) + (11*37). Okay, so I've just got a simple expression and I'm going to output that to a file. I'll call it calculation.txt. We know how to redirect our output.
Now I'm going to use bc, the calculator that we used in the last chapter. Before we used it from the command line, but instead I'm going to say hey, instead of using what I type, instead take the contents of this file and pass those into bc, and bc happily takes those and returns a result to us. So I think it's a pretty straightforward thing, but there are two important points that I want to make sure you understand. One is that we can both redirect the input and the output. So let's say for example, if I do sort < fruit.txt, the output right now is going to come to my screen.
If instead I want it to go somewhere else, well then I need to direct it as well, sorted_fruit.txt. Okay, see what it's going to do? The input is the fruit.txt; the output is the sorted_fruit.txt. It's the same thing as if we actually had sort and we piped in fruit.txt) > sorted_fruit.txt. All right, that works exactly the same. It has the exact same results. The parentheses aren't necessary though because we always put the input first.
Right, it's always the input, then the output. So I just wanted to see that you can use them together and also that the input always needs to come before the output. I also want to make another point here that the arguments to these always have to be files. Okay, it's very important. We're working with files when we talk about this. You can't for example do sort < cat fruit.txt, right. That's going to read the file? No. We want the file itself, not what we get when we read the file piped into it. It says no such file or directory is cat.
It's expecting a file or a directory to be passed in there. Same thing if we say lorem_ipsum.txt piped into wc as output, right. This won't work either because in this case it expects it to have output and the file on its own doesn't have any output. We have to cat the file to have output and then we need to pipe it into a file. The receiver has to be a file. If I just do wc, not word count, that's actually a file called wc and it happily does it. ls -la and there we see we have a file called wc at the bottom.
Okay, so I just want to make sure that you understand the thing going in using the less than sign needs to be a file and the thing that's coming out of the greater than sign needs to also be a file. All right, so let's go ahead and remove that. The last thing, let's just show you if we use unique case an example. Let's take our sorted_fruit. txt now and let's output it to unique_sorted_fruit.txt. Okay, so that works and it should have sorted it. cat unique_sorted_fruit.txt. So we went through several steps to get that sorted_fruit doing it this way, right.
And we know that there is an option for sort that would let us do all in one step and make it unique. Certainly could use that. But what I am interested in really making point about is the way that we sort of use these files as intermediaries, right. We took a file, we sorted it, we saved it to a file. Then we take that sorted file, we passed that into unique, and we saved that off as another file. It would be great if instead we could take the output from one command and send it straight to another command without a file, right. We can't do that with these operators because these operators are expecting this and this to both be files.
Instead we need another tool to do that. We call that the pipe. That's what we will look at in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97106 Viewers
61 Video lessons · 84412 Viewers
71 Video lessons · 68620 Viewers
56 Video lessons · 101108 Viewers
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.
Your file was successfully uploaded.