Couple of quick shell tips

Okay these are bash goodies, so they’ll work in any environment. If you’re in a situation where you’re switching between two different directory paths over and over again, here is a quick tip
cd -
Another little annoyance that I’ve gotten around is when you want to edit a system file and you type in the command (ie: vi /etc/this/is/a/really/long/path/config.cfg), only to realize that you forgot to sudo. This used to mean that I would quit vi, curse, recall the command, insert a sudo infront of the vi command and try again….well here is the quicker way.
sudo !!
This will (re)sudo your last command. And if that’s not enough you can actually narrow the sudo down to the last command that started with a certain string.
sudo !apache
Which will look in the history file and (re)sudo the first command that contains the string “apache”.


4 responses to “Couple of quick shell tips”

  1. Technically speaking, “sudo !apache” will execute the last command that *starts* with “apache”, not the last command that *contains* “apache”.

  2. Hi Jim,

    Thanks for the correction. Obviously memory did not serve me well enough. Went back, actually tried it and corrected the entry.

    Thanks again,

  3. “cd -” is a nice one … and it seems to work with tcsh too …

    i wouldn’t put too much trust into “sudo !!”, on the other hand.

Leave a Reply