Let's talk about the Bash command history! The bash shell stores in the user's home directorye. 1
the history of the commands each user has run in a file. 2
The file is .bash_history by default. 3
The number of commands then will be stored in the history file is controlled by an environment variable 4
called HISTFILESIZE (written in uppercase letters). 5
Remember that in Linux, everything is case sensitive. To print the value of a variable run: echo 6
dollar sign and the variable name, 7
in this case HISTFILESIZE. 8
This is the variable, and we notice that this Linux system saves 2000 commands into the history 9
file; you can print out the entire bash history to the screen by running a single command, which is history. 10
I'm running history. 11
These are all the commands saved in history. 12
You are seeing a list of commands in our history, along with a number to the left of each. 13
The command with a 1 next to it is the oldest command in your Bash history, while the command with the highest 14
number is the most recent one; and of course, the most recent command and run was history. 15
Note that running the history command doesn’t mean listing the entire contents of the history file. 16
There is another variable called HISTSIZE that controls how many commands from your bash history will 17
be stored in the memory. Let's see its value! 18
and it's 1000; so let's draw a conclusion: bash will save maximum 2000 commands in the 19
.bash_history file, in the user's home directory and maximum 1000 commands in the memory . 20
These are the commands displayed by the history command. 21
So the history command will display only half of the commands saved in the history file. 22
Also note that the history file is updated only when the user that run the commands logged out. I'm running 23
the command ip address show. 24
The command is displayed by the history. 25
Look, this is the command, but it's not in the history file. 26
It's not there; now I'll logg out. 27
I'm logging in again. 28
I'm opening the terminal again and checking the history file. 29
And the last command is in the history file. 30
To run a specific command from the history by it's no use an exclamation mark and the command 31
number. 32
For example, I want to run the 17th command from my history and I'll type an exclamation mark and the 33
number 17. 34
And to rerun the last command type two exclamation marks. 35
This has the same effect as pressing the Up arrow once to view the previous command and then pressing 36
Enter. 37
You can also refer to a command from a certain number of lines, back in the bash history. 38
For example, !-7 would run the last 7th command. 39
The command was rm1 and it was an error. 40
Let's try another example. 41
I want to rerun the WHO command; so an !-3 42
And the command was executed. 43
To search for a command in your history and run it, type !command_name 44
For example, I want to run the last ping command and to do that I'll type an ! 45
and the command. 46
And I'm hitting enter and it's running the last ping command. 47
I'm pressing control C to stop the ping. Note that this can be also dangerous because you don't know 48
exactly how the command was executed and there's the risk of executing another command. 49
What you can do is append :p to the above expansion and bash will print out the command to the 50
terminal without running it. 51
This is useful if you want to confirm that you've selected the correct command before running it, like 52
this. 53
It's just printing the command, it's not running it. And if the command is correct, I can run it. 54
Let's move on and see how to walk through the command history. Control and P or up arrow are used to 55
go to the previous command in the command history; press the shortcut multiple times to walk back through 56
the history and control N or down 57
arrow will go to the next command in the command history; press the shortcut multiple times to walk forward through 58
the history. 59
Bash also has a recall mode you can use to search for commands 60
you've previously run; press control and R and the start typing a command to search your Bash history 61
for it. 62
I am pressing control and R and I'm typing pi and it has found the command. When you find the 63
desired command you press enter or control and P to run it. 64
I'm pressing control and R again, type cat and now I do not want to run the command and I'm pressing 65
control and G to leave the history searching mode and clear the line. I'm pressing control and G. 66
There may come a time when you want to remove some or all the commands in your history. 67
If you want to delete a particular command enter history - D and the line 68
number. 69
For example, I want to remove this command. 70
So history - D and the line number. 71
The command was removed from the history, it's not there anymore. 72
To clear the entire content of the history file, execute history -c. The entire history was removed 73
It's empty. At the end of this lecture 74
I have a small homework assignment for you. 75
The command history is a very powerful feature of Bash and using it efficiently saves you a lot of 76
time. 77
However, there are moments when you want to run a command without saving it to history; let's suppose 78
you want to copy or view a file 79
you are not supposed to, connect to another machine 80
you are not allowed and so on. 81
How can you run the command without leaving any trace? 82
Think about it! 83
Try different ways to manage that and even search on Google for a solution. 84
Will take a short break and in the next lecture I'll show you how to do it.