Use the History Command Effectively to Make Your Work Easier

0
4363
Magnifying glass searching code
Linux users have to make use of the command line at one time or the other. The History command saves them from repeatedly typing certain commands. This article will help readers to leverage the History operations available.

While working with the GNU/Linux environment, one is forced to spend some time on the command line. One is likely to repeat common commands often, and typing variations on those commands even more frequently. This can be good practice in the beginning but later it gets annoying and the repetition of commands slows one down.
Luckily, we can save some time with the use of the History command and its variations.
In this section, we will look at how we can explore this command. For this demonstration, I am using Ubuntu 14.04 LTS, but the process is the same for most GNU/Linux distributions.

Using the History command
The GNU/Linux History library is able to keep track of all the previously executed commands and this information can be helpful in writing new ones and executing them. The History command can be used to list a log of the commands you have typed.
This log is called the ‘history’. And the command used to access it is history.

[bash]$ history
599 cat .bashrc
600 nautilus
601 vi
602 ls -l

In the above output, the former parameter is the command number in the History file and the latter is the actual command.
This will list all the previously executed commands.
Typing ‘history n’ will list the last ‘n’ commands from the log.
The following command will list the last five commands from the history log.

[bash]$ history 5
599 cat .bashrc
600 nautilus
601 vi
602 ls -l
603 history

The following are some keys to scroll through the history and jump to either end:

UP arrow key: Scrolls backwards in history.
CTRL + p: Scrolls backwards in history.
DOWN arrow key: Scrolls forward in history.
CTRL + n: Scrolls forward in history.
ALT+Shift+.: Jumps to the end of the history.
ALT+Shift+,: Jumps to the beginning of the history.

Variables configuration
The behaviour of the history log file is controlled by a few variables and these can be found or can be added to the .bashrc file. So let us have a look at the .bashrc file. This is present in the home directory. To view its contents, open a terminal and fire up the cat command followed by the name (i.e., cat .bashrc) or simply navigate to the home directory and press CTRL+h and open it with the text editor. Let’s review some of the variables.

HISTCONTROL: Now, it would make sense if you keep some of the variables’ values as they are, by default, like the value of the variable HISTCONTROL=ignoreboth (which will avoid duplication of commands) and shopt -s histappend (which will append newly executed commands to the previous history).
Although HISTCONTROL=ignoreboth is good enough for us, some of the other possible values for HISTCONTROL along with the syntax are:

[bash]$ export HISTCONTROL=ignoredups

(To eliminate duplicates, set HISTCONTROL to ignoredups)

[bash]$ export HISTCONTROL=erasedups

(To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups)

[bash]$ export HISTCONTROL=ignorespace

(When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace and typing a space in front of the command)
HISTSIZE and HISTFILESIZE: Some of the other variables in the .bashrc file are HISTSIZE and HISTFILESIZE.

  • HISTSIZE is the number of lines of the command to store in the history ‘list’ (i.e., in the memory)
  • HISTFILESIZE is the number of lines of the command to store in history ‘file’ (i.e., the actual history file)
    These variables are set to an integer value. To change these values, use the following command:
[bash]$ export HISTSIZE=2000
[bash]$ export HISTFILESIZE=2000

HISTSIZE can be used to disable the use of history.
If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below:

[bash]$ export HISTSIZE=0

Note that now history will not display anything.
HISTFILE: The HISTFILE variable can be used to store the history log to an alternate file. To do so, execute the following command:

[bash]$ export HISTFILE=.bash_alternate_history

Now the history will be stored in .bash_alternate_history.
HISTTIMEFORMAT=’%F %T: Typically, when you type history from the command line, it displays the previously executed commands, but sometimes it may be beneficial to display the time stamp along with the command. To do so, execute the following command:

[bash]$ export HISTTIMEFORMAT=’%F %T ‘

Now, on executing the history command, the execution date and time along with the command will be displayed:

[bash]$ history

16 2014-11-09 17:51:11 ll

17 2014-11-09 17:54:32 cd ..

13 2014-11-09 17:57:37 pwd

14 2014-11-09 17:58:47 history

HISTIGNORE: Sometimes you may not want your history to remember some basic commands such as ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, we have to be specific about the command that we would like to ignore from the history. Thus, executing the following command will ignore ls and ls -l commands:

[bash]$ export HISTIGNORE=”ls:ls -l:”

Note: 1) Sometimes you may be required to re-login for the changes to take effect.
2) To make permanent changes, add the variables with their values in the .bashrc file and re-login for the changes to take effect.

Event designators for history
Consider the history log for the upcoming event designator examples:

[bash]$ history

1 pwd

2 ls

3 cd Documents/

4 ll

5 nautilus

6 history

!n

The above executes command number ‘n’.

[bash]$ !1

pwd

/home/christmas/Documents

!-n

This executes current command minus ‘n’.

[bash]$ !-3

total 1648

drwxr-xr-x 5 christmas christmas 4096 Nov 6 11:48 ./

drwxr-xr-x 35 christmas christmas 4096 Nov 8 18:34 ../

-rw-rw-r-- 1 christmas christmas 8164 Nov 4 19:05 24914.png

-rw-rw-r-- 1 christmas christmas 27817 Nov 5 12:05 400_F_37625904_mNllKUnYH3cnVRKPlankmHzcz1zwlSjN.jpg

!!

This executes the previous command. This is similar to ‘!-1’.

[bash]$ !!

1 pwd

2 ls

3 cd Documents/

4 ll

5 nautilus

6 history

!string

…executes the most recent command preceding the current position in the history list, starting with the specified string.

[bash]$ !pw

pwd

/home

^string1^string2^

Quick substitution is of the form ^original^replacement^”. Repeat the last command, replacing the original with the replacement.

[bash]$ cd documents

bash: cd: documents: No such file or directory

[bash]$ ^documents^Documents^

cd Documents

[bash]$

Ctrl+r

Pressing Ctrl+r from the command prompt will display the reverse-i-search prompt as shown below.
Search previously executed commands with the Ctrl+r command. Once you’ve found the command you’re looking for, press Enter to execute it.

[bash]$ cat test

This is a test file.

(reverse-i-search)`test’: cat test [Enter]

[bash]$ cat test

This is a test file.

Browsing through history
To find all commands that involve a certain string, simply pipe it to grep.

history | grep cd

[bash]$ history | grep cd

3 cd Documents/

If you wish to view the history one page at a time, you can use the command below. Now, you can simply use the space bar to view one page at a time or use the down arrow to view one line at a time:

history | less

To view just the last 10 commands, you can use the following:

history | tail

tail when used with a -n option displays only the last n lines.

[bash]$ history | tail -5

29 man tail

30 man grep

31 nautilus

32 history | less

33 history | tail -5

History command line options

[bash]$ history

1 pwd

2 ls

3 cd Documents/

4 ll

5 nautilus

6 history

-c

The above code clears the history log.
syntax: history -c

[bash]$ history

[bash]$

-d -d offset

The code above deletes the history entry at position offset.
syntax: history -d 5 (Deletes the entry at position 5)

[bash]$ history

1 pwd

2 ls

3 cd Documents/

4 ll

5 history

-a

The above code appends the new history lines (entries in the current session) to the history file.
syntax: history -a

-r

The command line given above reads the current history file.
syntax: history -r

-w

The above command line writes out the current history to the specified file. This option is useful to export the contents of the history file.

syntax: history -w alternate_histfile.txt

-s

The arguments are added to the end of the history list as a single entry, as follows.
syntax: history -s argument.

LEAVE A REPLY

Please enter your comment!
Please enter your name here