Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using the command history

From: Unix for Mac OS X Users

Video: Using the command history

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.

Using the command history

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.

Show transcript

This video is part of

Image for Unix for Mac OS X Users
Unix for Mac OS X Users

82 video lessons · 26065 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 3m 57s
    1. Introduction
      1m 14s
    2. Using the exercise files
      2m 43s
  2. 32m 2s
    1. What is Unix?
      7m 27s
    2. The terminal application
      4m 23s
    3. Logging in and using the command prompt
      5m 19s
    4. Command structure
      5m 22s
    5. Kernel and shells
      5m 25s
    6. Unix manual pages
      4m 6s
  3. 15m 58s
    1. The working directory
      2m 49s
    2. Listing files and directories
      3m 59s
    3. Moving around the filesystem
      4m 58s
    4. Filesystem organization
      4m 12s
  4. 1h 4m
    1. Naming files
      5m 41s
    2. Creating files
      2m 19s
    3. Unix text editors
      6m 39s
    4. Reading files
      5m 35s
    5. Reading portions of files
      3m 27s
    6. Creating directories
      2m 40s
    7. Moving and renaming files and directories
      8m 32s
    8. Copying files and directories
      3m 7s
    9. Deleting files and directories
      3m 38s
    10. Finder aliases in Unix
      4m 10s
    11. Hard links
      5m 30s
    12. Symbolic links
      6m 36s
    13. Searching for files and directories
      6m 32s
  5. 34m 58s
    1. Who am I?
      4m 3s
    2. Unix groups
      1m 52s
    3. File and directory ownership
      6m 41s
    4. File and directory permissions
      4m 27s
    5. Setting permissions using alpha notation
      6m 49s
    6. Setting permissions using octal notation
      3m 49s
    7. The root user
      1m 57s
    8. sudo and sudoers
      5m 20s
  6. 52m 34s
    1. Command basics
      4m 4s
    2. The PATH variable
      4m 13s
    3. System information commands
      3m 40s
    4. Disk information commands
      6m 8s
    5. Viewing processes
      5m 0s
    6. Monitoring processes
      3m 36s
    7. Stopping processes
      3m 19s
    8. Text file helpers
      6m 50s
    9. Utility programs
      7m 28s
    10. Using the command history
      8m 16s
  7. 20m 39s
    1. Standard input and standard output
      1m 24s
    2. Directing output to a file
      4m 13s
    3. Appending to a file
      2m 44s
    4. Directing input from a file
      5m 28s
    5. Piping output to input
      4m 40s
    6. Suppressing output
      2m 10s
  8. 41m 28s
    1. Profile, login, and resource files
      9m 11s
    2. Setting command aliases
      6m 59s
    3. Setting and exporting environment variables
      4m 54s
    4. Setting the PATH variable
      6m 10s
    5. Configuring history with variables
      6m 17s
    6. Customizing the command prompt
      6m 5s
    7. Logout file
      1m 52s
  9. 1h 25m
    1. grep: Searching for matching expressions
      5m 21s
    2. grep: Multiple files, other input
      4m 28s
    3. grep: Coloring matched text
      2m 57s
    4. Introduction to regular expressions
      3m 22s
    5. Regular expressions: Basic syntax
      3m 19s
    6. Using regular expressions with grep
      5m 20s
    7. tr: Translating characters
      8m 17s
    8. tr: Deleting and squeezing characters
      5m 30s
    9. sed: Stream editor
      7m 45s
    10. sed: Regular expressions and back-references
      7m 8s
    11. cut: Cutting select text portions
      7m 42s
    12. diff: Comparing files
      4m 35s
    13. diff: Alternative formats
      4m 30s
    14. xargs: Passing argument lists to commands
      7m 25s
    15. xargs: Usage examples
      7m 59s
  10. 42m 25s
    1. Finder integration
      4m 45s
    2. Clipboard integration
      5m 5s
    3. Screen capture
      3m 42s
    4. Shut down, reboot, and sleep
      3m 34s
    5. Text to speech
      2m 36s
    6. Spotlight integration: Searching metadata
      3m 41s
    7. Spotlight integration: Metadata attributes
      4m 24s
    8. Using AppleScript
      5m 23s
    9. System configurations: Viewing and setting
      5m 51s
    10. System configurations: Examples
      3m 24s
  11. 1m 26s
    1. Conclusion
      1m 26s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Unix for Mac OS X Users.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.