Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last movie I told you that the standard output in Unix is going to be to the screen and we can see that. Notice that I'm inside my user directory, inside Unix files and I'm just going to type sort and fruit.txt. This is the command we did before, just sorts this list of fruit. Now I didn't actually change the file itself, all right. Sort just took it and the output of that command was to my screen. Not back to that file. Just simply output it to the screen and that's what we're seeing there. What we want do now is change the output. We want it to go somewhere else.
The way we do that as we say sort fruit.txt and then after that we use the greater than sign or an arrow pointing to the right. Essentially that's the way we're directing the output and we want to direct it to a filename and the file doesn't have to exist already. We're going to create a new file, sorted_fruit.txt. So that's it. That's all there there is to it. We simply say our instead of sending this information to the screen direct it to sorted fruit.txt. Hit Return, and I don't get any output because the output didn't come to my screen anymore but if I do ls -la we now see that there's this sorted_fruit file and if we take a look at that sorted_fruit file there it is, there is my output.
So extremely useful, especially for something like sort like that. Then we actually have the sorted version of the file and now we can do something like call unique on that file right. Before we saw the unique didn't work unless the results were sorted . Now we have the ability to do that. Now we could save this unique version if we wanted, right. Go ahead and try that your own. Save it to something called unique sorted_fruit. So any command that we typically get output back to the screen we can actually have be directed to a file. So for example let's say "Hello World" right.
echo, and instead echo coming back to me let's put that in the file, right, hello_world.txt. It did it. We did do ls -lah, right. We saw that command before. Normally that returns our directory listing. Let's save that to our file, content.txt. Let's try history. We just saw how to work with history. Do recent history.txt. So you can see when I just do ls -la you can see that all these files have now been created and you could do a cat on any of them and you'd see the content and it's exactly what would've come your screen but now it's going to a file instead.
Let me show you another one. We did the banner command earlier. Let's do banner "Hello World" and let's save that also to hello_world.txt. Now notice we already have an existing file called hello_world.txt, which just contains the text hello world. So what do you think what we'll get when we take a look at that file now? It's very long. Well let's scroll up to the top of it just to show you that it completely replaced what was there, right. It is destructive, so it does overwrite. So you're essentially saying make this output completely take over this file.
All right, we're going to learn how to append to our file in the next movie, to add more content to it. What this does is replaces it, okay. So very important, make sure that you understand that otherwise you might accidentally target a file that you don't mean to. Let me just show you a classic use case for this which is, if we do, let's say we have two files here and I have a file called new file.txt. It just has a couple lines in it, and I have a another version of it called newer_file.txt, right. Different text to that. Notice the lines that are in each one.
Cat you remember concatenates, right. So new_file.txt concatenated with newer_file.txt and let's put the output to joined.txt, okay. So there it is. It outputs those two concatenated together and directed the output to this file called joined. Now if we take a look at joined, you can see that we get both of those together in one file. And as I noted before, of course if we go up and we change the order of those, let's edit this so that we take new and newer and reverse them.
Now if we do cat joined you'll see that it flipped the order of them and replaced the old one. The old one is completely gone. So again it overwrote that one destructively. If we instead want to append the file, to add to it, we need to do something else and that's what we'll see how to do in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
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.