nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Reverse Search For Bash History

This is a discussion on Reverse Search For Bash History within the Getting started tutorials forums, part of the Linux Getting Started category; Hello My friends, Not sure if you were aware of this great bash function that I use daily in my ...

Register free or login to your existing account and remove all advertisements.


Go Back   nixCraft Linux Forum > Linux Getting Started > Getting started tutorials

Linux answers from nixCraft.


Getting started tutorials So much to read, so little time! If that is your problem, we have solution. Read our FAQ and tutorials to help you cut through the clutter of information overload. Only members of "contributors" group can post new tutorials. Other members can just reply to thread.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-29-2009, 11:31 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 525
Thanks: 54
Thanked 66 Times in 59 Posts
Rep Power: 8
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Reverse Search For Bash History

Hello My friends,

Not sure if you were aware of this great bash function that I use daily in my job as a linux systems administrator.

This is called "Reverse I Search" this is tremendously useful when a system administrator needs to search for a previously typed command.

e.g.
About an hour ago I typed a command on my mac book pro into my bash terminal. This command was a difficult command to remember that cleared cache on the system.

Code:
excalibur:~ jasonralph$ dscacheutil -flushcache
Now I have typed about 25 other commands since I typed the command above. I know need this command again, and I can't remember the spelling or command name. However I remember that the command started with dsc.

From the terminal I hold ctrl and r. This invokes the reverse search. Next I type the letters that I remember from the command "dsc". Bash will automatically display the command.

Code:
excalibur:~ jasonralph$ 
(reverse-i-search)`dsc': dscacheutil -flushcache
Now press enter to execute the command that the search has found.

Have fun and hope this helps.

Jaysunn
__________________
Give a man a fish, feed him for today.
Teach a man to fish, he will feed himself from now on.

Last edited by jaysunn; 11-30-2009 at 12:19 AM. Reason: changed title
Reply With Quote
  #2 (permalink)  
Old 12-02-2009, 11:07 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
OS: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 643
Thanks: 10
Thanked 11 Times in 10 Posts
Rep Power: 9
rockdalinux is just really nice rockdalinux is just really nice rockdalinux is just really nice rockdalinux is just really nice rockdalinux is just really nice
Default

Code:
command-name
Run the last command-name
Code:
!!
Another
Code:
cp /path/to/a/long/file/dir/name/file1 /go/to/dest/file2
to call 1st arg
Code:
echo !!:1
to call 2nd arg
Code:
echo !!:2
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
The Following User Says Thank You to rockdalinux For This Useful Post:
jaysunn (12-03-2009)
  #3 (permalink)  
Old 12-03-2009, 06:29 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 525
Thanks: 54
Thanked 66 Times in 59 Posts
Rep Power: 8
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

@rockdalinux

Sweeeeeeeet.

I was never aware of that one. Also this list can go on forever so please join.

Regards,


Jaysunn
__________________
Give a man a fish, feed him for today.
Teach a man to fish, he will feed himself from now on.
Reply With Quote
  #4 (permalink)  
Old 12-03-2009, 06:06 PM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
OS: Suse, RHEL, Vista
Posts: 188
Thanks: 10
Thanked 7 Times in 7 Posts
Rep Power: 7
sweta has a spectacular aura about sweta has a spectacular aura about
Default

Getting dir name or only a file name from a long path is also pretty handy when you use rsync or scp / ssh and friends

Code:
rsync /nas/a/x/y/foo/bar.txt root@server1:/dest/to/path
No I want only dir name
Code:
rsync -a !!:1:h !!:2
Get filename but leaving the dir:
Code:
cp /path/to/logn/dir/foofilenameverylongfilename.txt /mnt/floppy
oh no foofilenameverylongfilename.txt is in current dir:
Code:
cp !!:1:t !!:2
and the best one find and replace arg:
Code:
scp user@server1:/path/to/alongdir1/file.txt user@server2:/dest
on no dir1 must be dir2:
Code:
scp !!:1:s/dir1/dir2 !!:2

FYI, they are called history modifiers.
__________________
Reply With Quote
The Following User Says Thank You to sweta For This Useful Post:
kumarat9pm (12-04-2009)
  #5 (permalink)  
Old 12-04-2009, 04:10 PM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 334
Thanks: 12
Thanked 13 Times in 11 Posts
Rep Power: 4
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default History modifiers are cool

Thanks for sharing

though this is known to every just i want to share.

~/.bash_hisroty --contaions all your commands history
to check history size

echo $HISTSIZE

to change default history size(which is 500 in most of the linux flavors) to 20 commands history

HISTSIZE=20

To clear history

history -c
__________________
Thanks,
Surendra Kumar Anne
Ubuntu: Simple, Stylish and Striking..!
Linux: Fast, friendly, flexible and .... free!
Support Open source.
Reply With Quote
  #6 (permalink)  
Old 01-05-2010, 09:11 PM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 71
Thanks: 0
Thanked 11 Times in 11 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by rockdalinux View Post
Code:
command-name
Run the last command-name
Code:
!!

I loathe 'features' that cause a key that normally prints a character to do something else. When I press !, I want ! to appear, dammit!

I remove its special meaning by clearing the histchars variable:

Code:
histchars=
To search history I use either ^R (reverse-i-search) or up-arrow, which I have bound to history-search-backward. This will find previous commands beginning with whatever I have already typed on the command
line.

Last edited by cfajohnson; 01-05-2010 at 09:15 PM.
Reply With Quote
Reply

Tags
bash , bash ctrl + r , bash history , shell


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Quit Bash Without Saving History jaysunn Getting started tutorials 4 09-02-2009 03:54 PM
bash history...suse 10.1 grogf Linux software 4 10-30-2007 05:42 PM
Command history ricc Linux software 1 04-26-2006 05:57 PM
shortcut linux command history Shell scripting 1 12-26-2005 07:22 PM
want a script keep backup of all users history. ricc Shell scripting 3 08-26-2005 01:30 AM


All times are GMT +5.5. The time now is 02:57 PM.


Powered by vBulletin® Version 3.8.4 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2009 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38