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.
In this movie, we'll look at two other useful commands for reading files. They are head and tail. The difference is with both of these commands, they will read just a portion of the file. head displays the lines from the beginning of the file. This is useful for just peeking at the start of the file, let's say viewing the first ten lines. tail does the same thing but in reverse. It displays lines from the end of a file. This comes in very handy, when you want to look at files like access logs or error logs where the latest entries in the file will be added to the end. So for example, you can view the tail of a web server's access log to see the most recent requests made to the web server.
tail also has a very useful option, the f option which allows us to follow the file. It reads the tail of the file normally, but then instead of exiting back to the command prompt, it will watch for changes to the file. Whenever a new line is added, it will show us that line. We are following the file waiting for updates. It is a lot like following a news story for the latest updates. Let's see how we can use these. head lorem_ipsum. Voila, you see we just see those first ten lines I guess. We can change one of the configurations is to be able to change how many lines we see, but I am seeing the default there.
So you can see we can peek at the beginning of the file, and tail does the same thing but looking at the bottom of the file. I'll just clear the screen so we can see the difference. So there we are seeing the very bottom of file. Now, as I said, one of the places the tail is really useful is being able to follow a file. Let's try that real quick. Lets type tail -f and that will allow us to follow the file and let's try it with this newfile.txt that we created in the last movie. Just a nice simple file. There it is, there is the output. And you can see that it's just sitting there waiting.
We didn't get our command prompt back. That's because it's following the file. The process is still going on. To get out of it, in case you feel stuck, it's Control+C. That's one of the key combinations that's really common on Unix to be able to get out of a program to exit out, we'll be seeing that a little later on but for now, but for now just Control+C will get you back out into your prompt. Let's do it one more time so that we can see what it actually does. I am going to open up a new window with Command+N and let's just edit that with nano. So newfile.txt, there it is. I'll go down here. I'll make a new line here.
I'll say This is a new line in this file. I'll hit Return. Now exit out of it and save those changes to newfile and look over here, look what happened. It went ahead and actually echoed this line a second time. I think that's because of the line return that I put in, but this is a new line of this file. So it just followed the progress of the file. As new entries get made into there, we'll see what those are. So that's a really useful tool. And then Control+C to get out of there. If you want to try that out with tailing a file, one place that you can try that is to tail your system logs and that's stored in var/log/system.log. That's where that's contained.
I am not going to show you mine, but you can take a look at your system and see what's in there. If you have a web server running, you can do that on a Mac at least. Those are stored in apache2/access_log and you can watch requests come into the web server. Or I believe its error_log is where you would usually find the errors that come in. So you could tail both of those files and try that out. Now just as a quick summary again, you want to use cat for small files, you want to use less for large files, and you want to use head and tail when you wanted to take a peek at the beginning or the end of 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.