Sunday, April 27, 2014

history

  •   history command example

Linux bash history keeps every command a user typed in the command line terminal into a file named .bash_history.

Here is an example of Linux history command with no option running in Ubuntu server system command line terminal:
linuxcare@example.com:~$ history
1 ping 10.21.35.160
2 clear
3 ping -c6 192.168.0.253
4 ping -c3 193.168.0.253
5 ifconfig
6 cal
7 route ---help
8 man route
9 route
10 history

In the example above, number 1 is the oldest command and number 10 is the latest command that a current user has typed.

If you never clear the history file, you might see hundreds of commands that you typed including the latest 'history'. The bash history command would print only the latest page of the command history. However you can view bash commands history from the first page with pipe, like an example below:
linuxcare@example.com:~$ history | less


Let's see another example of bash history options. You can also use event designators to run command in the history using the ! key. See an example below:
linuxcare@example.com:~$ !8
man route
linuxcare@example.com:~$

The example above run number 8 command in the history file, which is the man route command. Here is a complete options available with event designators:
Event Designators
An event designator is a reference to a command line entry in the hisâ
tory list.

! Start a history substitution, except when followed by a blank,
newline, = or (.
!n Refer to command line n.
!-n Refer to the current command line minus n.
!! Refer to the previous command. This is a synonym for â!-1â.
!string
Refer to the most recent command starting with string.
!?string[?]
Refer to the most recent command containing string. The trailâ
ing ? may be omitted if string is followed immediately by a newâ
line.
^string1^string2^
Quick substitution. Repeat the last command, replacing string1
with string2. Equivalent to ââ!!:s/string1/string2/ââ (see Modâ
ifiers below).
!# The entire command line typed so far.


You can clear Linux bash history if it is too large or you don't want other people see what you were doing in the command line terminal. If you want to clear the bash history, use history command with -c option. See example below:
linuxcare@example.com:~$ history -c 

Linux history command can be really helpful when we are working in command line terminal. We can save a lot of times not typing regularly used command such as ls, cd and many more. However, keeping all bash history commands also might cause security threat to the system. For example, consider the root bash history file was read by unauthorised person. What you can do is always clear history after root session.
You can also delete line in history file. Let's look at the example Linux history file below:
1 updatedb
2 sudo updatedb
3 updatedb
4 sudo updatedb
5 locate mysql
6 cd /var/www/
7 ls
8 cat index.html
9 ls -l
10 cd ..

If I want to delete the history line number 4, what I have to do is to use history command with '-d 4' option. The -d option deletes history entry while the 4 argument is history entry line 4. See the example below:
linuxcare@example.com:~$ history -d 4

Now let's look at the Linux history file again:
1 updatedb
2 sudo updatedb
3 updatedb
4 locate mysql
5 cd /var/www/
6 ls
7 cat index.html
8 ls -l
9 cd ..
10 ls -l

The history entry number 4 has been deleted and the previous number 5 entry is now at number 4.
Here are other history command options that you can try:
  • -a — Appends to history file.
  • -n — Loads from history file.
  • -p — Performs history lookup/substitution.
  • -r — Reads history from a file.
  • -s — Adds new history entries.
That is all the Linux history command options regularly used in Linux administration.


0 comments:

Post a Comment