How to use Linux command history to avoid typing same command
Linux is free and open source operating system. Linux provides an utility called “Shell”. The shell is a command line interpreter which accepts the Linux command from the keyboard and delivers to kernel/os to execute the command. Most of the Linux based operating system (Ubuntu,Fedora,CentOs etc) comes with shell program. Let us run some Linux command before digging history command.
create a directory named as wikistack.
alen@Alen-Vostro-1550:~$ mkdir wikistack
Go to the newly created directory wikistack.
alen@Alen-Vostro-1550:~$ cd wikistack
Run history command on terminal
The history command will give the list of already executed commands on the terminal screen. The output is as shown in above figure. we can see that each command which we have run is having a number before it. This number is a numerical id of the command. we can use this number to run the command again. if you want to run “mkdir wikistack” command on terminal again then type following command on terminal.
The !1008 will run already executed command “mkdir wikistack“.
Running already executed command is fine with the method explained above, but if there are long history of commands then it is somewhat frustrating to traverse the entire history. There is alternative way for this case . Keep pressing Ctrl–R and it will traverse your history. After pressing Ctrl–R , you can type some initial command text, and keep pressing to search exact command which you want to run.
- To clear history run
alen@Alen-Vostro-1550:~$ history -c
2. How to force history command , not to remember particular command.
Using HISTCONTROL variable we can control the behavior of storing Linux command history. just export this variable on Linux terminal like below. ignorespace and ignoredups are two possible flags currently. ignorespace will ignore the command which starts with a space and ignoredups flag ignores the duplicate command history.
alen@Alen-Vostro-1550:~$ export HISTCONTROL=ignorespace:ignoredups
After exporting and setting HISTCONTROL variable, when we type a command after giving a space then the command will not be stored in history list. For example below command will not be stored in history.
alen@Alen-Vostro-1550:~$ gcc -v
Note: instead of export HISTCONTROL=ignorespace:ignoredups , we can use export HISTCONTROL=ignoreboth