Command history and automated repeats (bash)

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Command history and automated repeats (bash)

Postby viking60 » 04 Sep 2011, 13:05

We often put together complex commands -....... and then we forget them :confused
How can we reuse those brilliant pieces of (lacking) know how or how can we find them again? You know -that brilliant tip from the guru on that site that you forgot - needless to say you forgot the command itself......but it was something with urpmi :think:

Fear no more the

Code: Select all

history
command will come to your rescue +1
It will give you the list over all commands executed and in historical order:

Code: Select all

  990  ssh viking60-server
  991  sudo urpmi --auto-update -a
  992  ssh viking690-server
  993  ssh viking60-server
  994  sudo poweroff
  995  wpa_supplicant
  996  sudo wpa_supplicant
  997  cd /etc
  998  ls
  999  history
 1000  sudo urpmi --auto-update
 1001  ls
 1002  history

It does not care if the command was correct or not - this is the history right or wrong -basta!

Now i find that
sudo urpmi --auto-update -a
hard to remeber so I will just execute it by executing its number in the history table.
Like this:

Code: Select all

!991

And wonder of all wonders it is executed directly :B

Now this can be a bit dangerous because I do not get a peek at the command first - it just executes. And that could potentially :C
So how do I get to have a look at the command first?

Code: Select all

!991:p

Will do it! By adding the :p (for peek) you can have a look at it first.

To add the previous command you can do a:

Code: Select all

!!
or

Code: Select all

!-1
To add the command before that you can do a:

Code: Select all

!-2
and so on.
So to have a peek at three commands back what do we do?
You guessed it:

Code: Select all

!-3:p


But it gets better +1
I am lousy with numbers and I don't even remember what I did one command back :confused But I know it was starting with sudo urpmi so I do a:

Code: Select all

!sudo urpmi

And there it goes - doing the

Code: Select all

sudo urpmi --auto-update -a

If I want to have a look at the command starting with urpmi?

Code: Select all

!urpmi:p

Or the last command starting with pacman (Arch):

Code: Select all

!pacman:p
etc etc...
You do not even need to remember the entire command. If you want commands executed with ssh and only remember ss just do a:

Code: Select all

!ss:p

It will output something like this;

Code: Select all

$ !ss:p
ssh viking@viking60-server

I just needed to write this down in case I forget :coffee_smile:
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Command history and automated repeats (bash)

Postby rolf » 04 Sep 2011, 16:00

This is very good. Using konsole, where 'Ctrl-r' will step back in history, and having done several new installations, saving /home, recently, I think I run up against a limitation for going back, albeit konsole scrollback is set to unlimited and, I figure, this record is kept in my $HOME.

Also, 'Ctrl-r' seems to break down as soon as I typo, so that is not so nice. :f

It looks like your Tips and Tricks is more powerful, thanks! :B

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Command history and automated repeats (bash)

Postby viking60 » 04 Sep 2011, 17:38

And there we have another tip from our Guru:
Hit <CTRL>+r and enter the search word. It will have the same effect, and do an even more elegant search. Thanks! (Well... unless you do a typo :-D )
In fact I can even correct typos in my Mandriva 2010.2 :B :s
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"


Return to “Tips & Tricks”