blog

Software screen capture

Finessing the Command Line for Fun and Profit

by

As someone who spends a lot of time using the command line, I’m often repeating the same commands and jumping through the same hoops. Over the years I’ve learned a few lessons. Here are some simple tips to save you time in the ‘shell.

Folder bookmarking

For a single project, I may have a handful of directories I often need to switch to from the command line. Over time, this can become tedious. A solution might be to create aliases such as "cdbanana" to take me to the Banana Stand project base directory, but that itself is kind of messy.

"To", written by Mara Kim, alleviates this problem by allowing you to set bookmarks from folders you’re cd’ed into, and jump to them from anywhere else. The source is released under the GPL on Github.

Set a bookmark on the current directory using: to -b <new bookmark name>

Jump to your bookmarked directory from anywhere else:to <bookmark name>

Example:

bananaProj $ cd Web/protected
protected $ to -b banana
protected $ cd /some/other/folder
folder $ to banana
protected $

Bash shortcuts

You can use cd - repeatedly to switch to the previously-selected directory.

home $ cd radams
radams $ cd -
home $ cd -
radams $&lt;/pre&gt;
You can also refer to the previous directory in other commands, like:
&lt;pre&gt;radams $ cd ~/Documents
radams $ find ~- -name &#039;.bash*&#039;

Not the best example, but you see how it works.

You can also easily re-issue commands from your Bash history:

radams $ history | grep revert 
474  git revert e3ac6a088eef1502875d96d3b7d678b6740a3a24
radams $ !474

As soon as I hit enter, the entire command is entered and executed.

You can also enter !find, !grep and so on to re-execute commands from your history whose initial text matches the word after the exclamation mark. If it’s easier to find by an argument you passed to one of those commands, use a question mark after the exclamation, e.g. !?secret-doodle would match a mysqldump -u root -p"secret-doodle".

And finally, don’t forget about the built-in history search, which you’re sure to enter by mistake some day while furiously pounding the keyboard. Ctrl+R will prompt for a search, and when you find a command from your history you want to run, hit enter, or arrow left or right to edit it.

+ more