Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Using the command history


Unix for Mac OS X Users

with Kevin Skoglund

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.
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

please wait ...
Watch the Online Video Course Unix for Mac OS X Users
6h 35m Beginner Apr 29, 2011

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.

Topics include:
  • Moving around the file system
  • Creating and reading files
  • Copying, moving, renaming, and deleting files and directories
  • Creating hard links and symbolic links
  • Understanding user identity, file ownership, and sudo
  • Setting file permissions with alpha and octal notation
  • Changing the PATH variable
  • Using the command history
  • Directing input and output
  • Configuring the Unix working environment
  • Searching and replacing using grep and regular expressions
  • Manipulating text with tr, sed, and cut
  • Integrating with the Finder, Spotlight, and AppleScript
Mac OS X Unix
Kevin Skoglund

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.

Find answers to the most frequently asked questions about Unix for Mac OS X Users .

Expand all | Collapse all
please wait ...
Q: The exercise files for the following movies appear to be broken:

Is there something wrong with them?
These exercises include one or more "dot files", whose file names start with a period. These files are normally hidden from view by the Finder.  So that they would show up in the Finder, the period has been removed from the file names. Additionally, "_example" has been added at the end of the file name to make it clear that the file will not work as-is. To make the dot files usable, either:

1) Open the file in a text editor to view its contents. Note that it may not be possible to double-click the file to open it because there is no file extension (such as .txt).
2) Resave the file under a new name (usually by choosing File > Save As), adding a "." to the beginning of the file name and removing "_example" from the end.


1) Copy and rename the file from the Unix command line using the techniques discussed in this course. Rename the file by adding a "." to the start and removing "_example" from the end. Include the "-i" option to prevent overwriting an existing file unexpectedly.
Example:  cp -i ~/Desktop/Exercise\ Files/Chapter_07/07_02_files/bashrc_example ~/.bashrc
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

* Estimated file size

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


You have completed Unix for Mac OS X Users.

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


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts. provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now

A trusted source for knowledge.


We provide training to more than 4 million people, and our members tell us that helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about 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

Sign up and receive emails about 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.