Using bash history more efficiently | history Command Linux
The ability to maintain a history of previously executed commands is one of the most widely used features of the bash shell. Every system administrator is accustomed to using the inbuilt using bash history command to recall to some commands to be re-executed or just to check what commands were executed previously on the system. The bash shell stores the history of commands executed in a terminal session in the .bash_history file in the home directory of the user.
You might be aware how to access the shell history using the history command. In this article, we will share some tips and tricks to allow you to maintain your bash shell history efficiently and perhaps drastically improve your productivity as a system administrator.
So, let’s get started.
Access command line using bash history
To access the shell history simply type the history command. This will show you the commands available in the shell history up to the shell limit. We’ll talk about this limit shortly. You can also read the .bash_history file directly to view the history of commands executed previously. But note that this will not contain the commands you are executing in your current shell session because they will be written to the file only after you’ve gracefully exited the terminal session.
View only particular count of commands
You could pipe the output of the history command to the head and/or tail commands if you are interested in only a subset of the complete output. Or you can use the history command followed by the number of commands you’d like to see. Note that this number will be in descending order from the most recently executed command. For example, to view the last 10 commands in history, type:
Browse through history
To browse through the shell history, use the up and down arrow keys on your keyboard while you are at the command prompt.
Execute the last command in history
To execute the last command or the repeat the most recently executed command available in the shell history, just type double exclamation symbol (!!) at the command prompt.
Here’s a demonstration:
[sahil@arkit-centos:/tmp] $ ls check_roster_list.log hsperfdata_qscn nagiosxi vmware-root global-info.tmp lost+found service_account_password_check.log yum-sahil-kByu5Z [sahil@arkit-centos:/tmp] $ !! ls check_roster_list.log hsperfdata_qscn nagiosxi vmware-root global-info.tmp lost+found service_account_password_check.log yum-sahil-kByu5Z [sahil@arkit-centos:/tmp] $
From the above output, you could observe that I executed the ls command followed by !! which executed the ls command again as it was the most recently executed command in the shell history.
Execute nth command from the end of the shell history
To execute the nth command from the last command, we use the syntax !-n where n is the integer indicating how far back we’d like to go.
For example, to execute the 2nd command from last, type !-2.
[sahil@arkit-centos:/tmp] $ !-2 cd /tmp [sahil@arkit-centos:/tmp] $
Execute nth command in shell history
To execute the nth command from your shell history, use the syntax !n where n is the number at which the desired command is available in the shell history.
For example, to re-execute the command at number 923, type the following:
[sahil@arkit-centos:/tmp] $ !923 which git /usr/bin/git [sahil@arkit-centos:/tmp] $
Use the last argument of the previous command
If we wish to include the last argument from the previous command which could be a lengthy file name etc, we use !* to represent it in the next command.
Given below is an example:
[sahil@arkit-centos:~] $ ls /tmp check_roster_list.log hsperfdata_qscn nagiosxi vmware-root global-info.tmp lost+found service_account_password_check.log yum-sahil-kByu5Z [sahil@arkit-centos:~] $ cd !* cd /tmp
Copy the entire previous command and use it in next command
In an earlier example, we showed you how to re-execute the previous command from history by typing !! at the command prompt. We can in fact also use !! as a substitute of the last command in the shell history in the command currently being typed. Here is an example to demonstrate:
[sahil@arkit-centos:/tmp] $ file /boot/initramfs-2.6.32-642.13.1.el6.x86_64.img /boot/initramfs-2.6.32-642.13.1.el6.x86_64.img: regular file, no read permission [sahil@arkit-centos:/tmp] $ sudo !! sudo file /boot/initramfs-2.6.32-642.13.1.el6.x86_64.img /boot/initramfs-2.6.32-642.13.1.el6.x86_64.img: gzip compressed data, from Unix, last modified: Thu Feb 23 19:00:03 2017, max compression
Execute most recent command beginning with a particular string
Thus far we’ve shown you a few examples to recall commands by the numerical order in which they occur in the shell history. We could also recall commands by name. For example, to re-execute a command beginning with a particular string, we would type !string on the command line. So, if I wanted to re-execute the last command that began with the word sudo I would type !sudo as shown below:
[sahil@arkit-centos:/tmp] $ !sudo sudo file /boot/initramfs-2.6.32-642.13.1.el6.x86_64.img /boot/initramfs-2.6.32-642.13.1.el6.x86_64.img: gzip compressed data, from Unix, last modified: Thu Feb 23 19:00:03 2017, max compression
Perform a reverse search in history
Thus far most of the tips we’ve shown you involve typing in the history command first to view the command list and then re-execute the selected command from there. We could also perform a string search for our desired command by typing ctrl+r key combination on the command prompt. This will bring up a reverse i search, whatever you type in this prompt will be searched by in descending order i.e. latest first.
(reverse-i-search)`ls': ls /tmp
The above example demonstrates a search for the ls command. To execute the result of the search press the enter key or press the arrow key if you just want to type it out at the prompt. If you wish to continue searching then press ctrl+r again until you reach the desired match.
If you wish to clear the command line history and start afresh then type the following command
Using HISTCONTROL and HISTIGNORE variables
This is a shell environment variable using which we can instruct the shell history not to record commands that begin with space and record only the most recent occurrence of the command if it gets repeated.
To use the features we just describe, set the value of HISTCONTROL variable as follows:
echo "export HISTCONTROL=ignoreboth" >> .bash_profile . .bash_profile
This will enable HISTCONTROL settings immediately.
There might be some commands like ls or history that you’d never want to get recorded. For this purpose, we use the HISTIGNORE variable. For example, to ignore the history command from being recorded, type the following using bash history
Now, let’s try this out.
[sahil@arkit-centos:~] $ history 2 666 df -h 667 ls [sahil@arkit-centos:~] $ history 2 666 df -h 667 ls [sahil@arkit-centos:~] $
Time history command with HISTTIMEFORMAT
The HISTTIMEFORMAT variable allows us to add a time stamp to our using bash history commands.
Let’s demonstrate this on the command line.
[sahil@arkit-centos:~] $ history 5 675 Dec 29 2017 16:50:04> HISTTIMEFORMAT="%h %d %G %H:%M:%S> " 676 Dec 29 2017 16:50:07> uptime 677 Dec 29 2017 16:51:19> date 678 Dec 29 2017 16:58:04> uptime;who 679 Dec 29 2017 16:58:28> uptime
Control history length with HISTSIZE and HISTFILESIZE
- By default, the shell history is saved up to 1000 commands depending on the operating system.
- We can change this value by using the HISTSIZE variable to a modify the number of commands to be saved.
- The size of the .bash_history file is 1000 lines by default. To modify it, we need to change the value of HISTFILESIZE variable. Using bash history command.
This concludes our tips and tricks for the bash history. We hope that using these you will be able to optimize your shell history and become more productive on the command line. Using bash history more efficiently Linux history command.
Thanks for your wonderful Support and Encouragement
- Do Not Miss Interesting and Important Articles: Signin Now
- Download free PDF Books : Download Anything and Everything for Free
- Stay Connected with Us: Facebook Page | Twitt | Google Plus | LinkedIn | RSS feeds
- High Qualitfy Video Tutorials : All Videos
- Still You Have Question? : Ask Us
Click on above colored text to access appropriate page