49 views

Bash history powerful tips, Search, TAG and more

Looking for a specific previous command bash command history is very easy,

Control + R in the keyboard # Try it now.

Pressing Ctrl+R on the keyboard at a bash prompt would show a search prompt like this:

[developer@aws-askdevops~]$
(reverse-i-search)`':

Now you can start typing a part of your command, for example, I am typing git and then bash would show me the latest history line in which "git" is included

[developer@aws-askdevops~]$
(reverse-i-search)`git': git commit -m'bash_history'

Searching for match Ctrl+R for toggling among results

  • To match another keyword in history, keep typing it
  • To toggle the result line sets, press Ctrl+R
  • To match with the next previous result, press Ctrl+R again

Examples:

(reverse-i-search)`git com': git commit -m"Docker files"
Ctrl+R
(reverse-i-search)`git com': git commit -m'Docker project'

Tagging a Bash command for future use, using the comment (#)

There is another powerful way of utilizing bash search feature, look at the example below,

df -kh /var/www/html  #dfhtml

I just added a tag #dfhtml or comment to bash history, which would make it easier for me to retrieve this command set from bash history by just typing Ctrl+R then dfhtml. Whatever we type after the comment would not be executed.

History command line versioning using TAG

Adding a tag to history would be very useful when we need to:
- Use a very long command with arguments(no need to type or copy-paste all over again)
- Versioning between specific command sets
- Use it as a temporary alias

Bash history limits and fine-tuning it

By default, bash has a limit in the number of lines it remembers, for example in CentOS 7 I see it as below,

[developer@aws-centos7~]$ echo $HISTSIZE
1000

To make a change to this limit, you may set this in your .bashrc

vim ~/.bashrc
export HISTSIZE=25000

This would set the bash history size as 25000

Jan 6 by | 49 views
DevOpsRun.com is LIVE!

Articles for DevOps Geeks
MobaXterm Customization
CHMOD Explained!
Bash history powerful tips
Ubuntu bash from Windows
..