Unix Reference

history (shows your last commands)

Video

When using the command line on linux computers, you invariably end up typing the same command many times. The linux Bash shell keeps track of your commands in a history file, which you can see by typing history:

$ history
... (potentially lots of commands)
 5001  cd cs107/assignments/assign0
 5002  ls
 5003  cat hello.c
 5004  ls
 5005  cd
 5006  ls
 5007  ls -alrt
 5008  history
$

There are many ways to retrieve commands from your history:

Up/Down arrow keys (redo a previous command): use the up arrow key to go back through your commands one at a time, and down to go the other way through the list

!character: if you type an exclamation point (also known as bang) followed by a character or a string and then enter, you will run the last command that starts with that character or string. E.g.,

$ ls -a
.  ..  hello  hello.c  hello.c~  innerFolder  readme.txt  samples
$ cd ..
$ !l
ls -a
.   assign0  assign2  .do_not_look.txt  file2.txt
..  assign1  assign3  file1.txt     .this_is_hidden.txt
$

Typing !l runs ls -a again, because the previous command that started with l was ls -a.

Ctrl-R: If you hold down the control key, and then type "r", you can then start typing characters and the line will start being populated with the last command that starts with the characters you have typed. This is easier to see in the Video and you should try it yourself. Back to contents