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 ...


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 29-11-2009, 10:31 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 598
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
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 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
__________________
Have a look at what I have been working on
http://www.shellasaurus.com

Last edited by jaysunn; 29-11-2009 at 11:19 PM. Reason: changed title
Reply With Quote
  #2 (permalink)  
Old 02-12-2009, 10:07 PM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 703
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
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.
What's wrong? I hope I am not making you uncomfortable...

Never send a boy to do a mans job.
Reply With Quote
The Following User Says Thank You to rockdalinux For This Useful Post:
jaysunn (03-12-2009)
  #3 (permalink)  
Old 03-12-2009, 05:29 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 598
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
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 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
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #4 (permalink)  
Old 03-12-2009, 05:06 PM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
OS: Suse, RHEL, Vista
Posts: 199
Thanks: 12
Thanked 9 Times in 9 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 (04-12-2009)
  #5 (permalink)  
Old 04-12-2009, 03:10 PM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 439
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about 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 05-01-2010, 08:11 PM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 78
Thanks: 0
Thanked 14 Times in 14 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; 05-01-2010 at 08: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 02-09-2009 02:54 PM
bash history...suse 10.1 grogf Linux software 4 30-10-2007 04:42 PM
Command history ricc Linux software 1 26-04-2006 04:57 PM
shortcut linux command history Shell scripting 1 26-12-2005 06:22 PM
want a script keep backup of all users history. ricc Shell scripting 3 26-08-2005 12:30 AM


All times are GMT +5.5. The time now is 11:51 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 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