Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last movie we saw how we can redirect outputs, so that instead of being output to standard out which is our screen, instead it's directed to a file. In the process of doing that we saw that it actually overwrites the contents of the file. So we completely destroy whatever was in the file by doing it that way. What we're going to see now is how we can append to the end of the file. So we are still directing the output to a file, but we're just doing it without destroying what was already there. Notice that I'm inside my user directory and inside Unix files, and if I do ls -la you'll see the files that are there now. A lot of those are files that we just created in the last movie.
Let's begin by creating a list, nano people.txt. Let's just put a list of people's names in here, Lynda, Bob, Susan, Larry, and Anne. I'm going to hit Return. I want to show you something that nano does here. If I hit the down arrow you'll see it actually gives me one extra line. So there is an automatic sort of implied extra line there. I'm going to remove that, okay. So just make sure that we have only one line return after Anne then I'll hit Ctrl+X. Yes save the changes to people.txt.
So let's take a look, people.txt, and there is the list of people. Now let's say we want to add Claire to the end of this list. Well we can do echo "Claire" and if we do it this way, we know what it will do. it'll completely erase our old list. What we want instead is to append it to the end of the list. It's very easy. We just use two of those greater than signs. Append it to the end of the list instead of getting rid of it. So now it echoes that line there. If we now say cat people.txt, now we see Claire at the end of the list.
I want to add John to the list, same thing, John, and add it to people.txt. Now John is added to the list. That's it, that's all there is to it. Let's just try another example. We were working with a new file.txt and we were concatenating it before with newer_file and putting in a joined. I'm just going to put new file in there, so that replaces whatever was there. Now let's do it again, but this time let's do newer file and append it. Notice the difference between those two. I'm taking a different file and I'm appending it to the end what was there.
That's the same thing as if I concatenated those together. And then just let's go ahead and do new_file in there one more time, okay. So you can probably guess what we'd expect to be in there. If we take a look at that file, we have new_file then newer_file, then new_file, okay. So it's really all there is to it. It's pretty easy as far as the concept goes. The hard part is just making sure that you have the discipline to remember to use the greater than sign, just one of them, when you want to completely replace the content and two of them when you want to append it.
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.