- [Instructor] Using the shell's history can keep you from
having to type in commands over again.
If you want to just bring up the last command,
you may want to use your up and down arrow keys.
If you want to see what commands you've typed in,
use the history command, by typing in history into a shell.
History shows the most recent commands we've typed,
up to the shell limit.
The limit is usually 500 or 1,000 items,
depending on the configuration.
If you want to bring back the entire last line
from the history, type in !!
and hit enter.
In my case, it's the history command,
which we just typed in.
If you want to execute the command
four lines from the bottom,
you'll use !-4
and hit enter.
Type in history again to see the list.
We can see that four lines from the bottom
was the clear command for me.
For you, it may be something else.
If you want to run a specific line,
just specify the number after the !
For instance, to run the command on line seven,
you would type in !7 and hit enter.
If you do not have a seventh item,
choose a different number in your history list.
You can also run the line starting with something.
For instance, to run the most recent line
that starts with ls, type in !ls and hit enter.
If you want to run a command
with a previous line's arguments and options,
you can append !* to the command name.
To set this up, let's type in ls /var/log and hit enter.
To cd to the /var/log directory in our last command,
we could type in cd !* and hit enter.
Now let's type in history to see what happens.
According to the history command, we had ls /var/log,
followed up by a cd /var/log,
when in reality, we typed in cd !*
If you want to insert the entire previous line
of our history on the command prompt,
we can use !!
To set this up, let's try to view a file
without proper permissions.
Type in cat /etc/shadow and hit enter.
And it gives us a Permission denied message.
Append the entire previous line after sudo using !!
by typing in sudo !!
This will take the entire previous line,
which is cat /etc/shadow
and append it to the end of this line.
Hit enter, type in your password,
and we can see that it worked.
Type in history again to see what was done.
You can see that we typed in cat /etc/shadow
then the sudo !! ended up executing sudo cat/etc/shadow.
The negative to these commands is that we need to know
where in the history we typed the command,
and thus, have to type the history command first.
A faster way to search through previous commands
is using the reverse i search.
Press ctrl + r,
and you'll see your prompt changes to reverse i search.
Now type in ls and you'll see
that the last line that starts with ls shows up.
If you want to run this line, press enter.
If you'd like to edit the line before running it,
press the right arrow key or left arrow key.
If you want to step through a reverse i search,
just press ctrl + r again.
For instance, press ctrl + r,
type in ls, and then if you want to go to the next item,
press ctrl + r again.
When you get to the one that you want,
hit enter.