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.
In this movie, we are going to learn how to utilize the Unix command history. Early on in this training, we learned how to use the up arrow to go back and review commands that we had issued previously. We can then either reissue those commands or make edits to them. But what you may not have thought about is how does Unix remember those commands? Because even if we close the Terminal window, even if we shut down our Mac and reboot it, Unix still remembers what our previous commands are. So it must be keeping track of them somewhere, and it is. It does it inside a file and that file is inside our user directory, ls -la.
You can see it here. It's a dot file, bash_history. And this is where the bash shell stores its history of commands. We will take a look at the contents of that file. You will see that it lists our previous commands. It's just one line per command. Now yours is probably a lot longer than mine. I actually went in and edited mine to make it a lot shorter. I want you to notice something. If we hit our up arrow now, our last command was actually to look at the bash history and the command before that was to see what's in the directory and the command before that was to change to our user directory, but none of those are being reflected in this bash history file.
That's because bash writes to the bash history file whenever it quits. So once we quit the sessions, close out this window, it'll write our reset commands to that bash history file, and the next time we come in, bash will read the bash history to reload those commands and make sure that it has them in memory ready to go. Until then, until we quit, it just keeps them and holds on to the main memory. So, essentially these commands that we see here in this list are the commands that were issued prior to our current login. If we want to the full list of commands, then we need to issue the history command. Just simply history and then we will see a list.
What we are seeing here is a list of all previous commands, those that were in the bash history, plus the more recent ones. Notice that the list is numbered. We can use these reference numbers to do that command again. This is much easier if we have done a whole lot of things than hitting the up arrow over and over and over till you find the command that you did a long time ago. Then chances are if you're like me, you still will end up using that up arrow a lot of the time. There is something that was done a long time ago, you know, 50 commands ago, then just go to your history and look for it this way. So how do we reference these numbers? Well, there are some shortcut commands to work with the history and you can recognize them all easily, because they all start with the exclamation point.
Exclamation point followed by the number of the command that we want to execute. So for example, if we want to execute the calendar command, it's just !1, and there it is. It runs it. Notice that it first tells us what command it's actually doing and then it goes ahead and does it. Now, also I want you to notice, if you hit the up arrow, that we don't see the !1 anymore. That was just a shortcut, right. What we actually see is the command itself, which is really useful, because then, for example, we can edit this. So we wouldn't be able to edit it if we didn't have the actual command itself.
So I can make an edit here. I am just going to hit the down arrow without hitting Return. Okay, it's very important, because I want to show you something. So I have made a change to this line. Hit the down arrow again. Now let's type history one more time. Notice now that it does show that command that I just executed a moment ago, but it changed it. Now it says Cal 6 2008. So hitting the up arrow, making edits to command, it does change them in the history. And it includes an asterisk next to the number to let you know that it was edited. So even though that command never ran, it's in our history that way. All right, let me show you the next shortcut, which is to use the exclamation point and then a minus sign and then the number of commands back that we want to go.
So for example, if I want to run a command that was two commands ago, that one command back would be my history. Two commands back could be that cal command. I am going to hit Return and you will see what it does. It actually did this command, Cal 6 2008. Even though that's the amended command, it still does it. But the minus 2, you can do minus 3, minus 4 whatever, it's always relative to where you are now. If I do minus 2 now, it won't give me that cal again, right? It's a moving target. Now two commands back would be the history command.
So it's relative to where you are, how many commands back, but if you have done something, you do one command, you do two commands, you do three commands, you do the fourth command, and say, I want to do that, first one again, well, it's real easy just to say all right, I want to go back four commands and do that command that I did a moment ago. In most cases it's probably going to be easier to use the absolute id value instead of this relative one. Another nice shortcut is that we can actually use the exclamation point followed by alphanumeric characters and it will go back and try and find the command that begins with those characters.
So for example, you see up here that I have command number 10 is to evaluate this expression. If I type expr and then hit Return, that was the most recent command I did that began with expr. Now I didn't actually have to type the whole command. I could just type ex and it would have done the same thing. It again tells me what command it's issuing and then gives me the results of that command. That's a nice handy trick. So let's say that you added a file using nano and then you do a few other things and then you say oh you know what, I needed to edit that file again, well, if you haven't done another nano since then, you could just type !nano and you will be re-added in that file again.
Keep in mind of course, if you move around and change directories, then that command may not work the way it used to, because it's always being executed from where you are in the file system. So you might have to modify the path to the file that you want to edit in order to get it. There's another very useful shortcut that I use all the time and that's to use exclamation point, exclamation point. This is the same thing as doing exclamation point minus 1. Where it's most useful is let's say we were doing a chown and we are going to chown on unix files. Inside there we had a file called ownership.txt.
That in the ownership movie, we were talking about changing ownership of that file, but remember, operation not permitted. I can't do that. I need to do it with sudo in front of it. So it's a classic use case. We say sudo!!. Right? Saying no, I really mean it. Do the same command, but this time prefix it with sudo. See how that works? So it doesn't just execute the command. It's actually a reference to the command. I will use the up arrow, so I can change that ownership back. Notice that once again the command itself has changed so that now sudo is in front of it.
We are actually seeing the actual command itself, not sudo!!, which is perfect for a use case like this, where I want to go back and edit the line and re-execute it again. Another one that's really handy is exclamation point followed by a dollar sign. What that does is it references the arguments from the previous command, so let's say for example that I do nano and let's go in to my unix files and I am going to edit the file fruit.txt. Here is my file. I go in, I make some changes. When I am finally done, Ctrl+X and I exit out.
Then let's say I go, you know what, did I make change correctly? I want to look at my changes. I want to cat that file. Well, I could type the whole thing again, but I also can use exclamation point dollar sign and what that references is the arguments to the previous command. So, not the nano part, but the unixfile/fruit.txt. And there it is. See, here is the command that it actually executed. It shows you that first. That's what it did. it grabbed just this last part here and used it with the new command.
That's super handy because a lot of times, you want to have a file and you want to do several things to that file, right? You might want to move it and then you might want to copy it and then you might want to edit it. You can just keep using this to keep referring to that same file without typing it over and over again. You can read the man pages on history to see some of the other things you can do with it. I just want to give you a couple of options that are helpful. Let's say we have history and let's say that I want to get rid of a line. Let's say that this line 27 that's here. We have it twice. I want to delete that out of my history,. You can do that with -d and then the number, and now I want to do history again.
You will see that it's gone and everything shifted up one. So the numbers are not absolute. Each command is not completely bound to these numbers, but if we delete it, they shift up so that it doesn't leave a space. And if we want to clear all of our history, you can use history-C. That will clear it. Or you can just get rid of that bash history file. You can just remove the file. So just so you have a quick reference, here is a chart that shows where all of those shortcuts are, just so you can see them and refer to them. Make sure that you understand what they all do, and try and get used to using them, because they will make life easier.
You may feel a little odd at first, but if you get used to using them, I think you will find that they really speed things up for you a lot.
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.