Zerø Wind Jamie Wong

Commandline Tips and Tricks

Alas, I have been stricken with the plague of half-finished posts and projects. But, as I feel the need to have at least one post per month, here’s some stuff you might find useful.

I forget where I discovered this, but commandlinefu.com is an awesome site.

It’s so awesome in fact, that I think the productivity gains I’ve gotten from it almost outweigh the time I wasted looking through the site and testing it.

Here are some of the more useful ones from the site and a few I learned at work and elsewhere.

Change to previous working directory

From: Change to the previous working directory @ commandlinefu.com

$ pwd
/Users/jamiewong/code/stackoverflow

$ cd /usr/bin

$ pwd
/usr/bin

$ cd -
/Users/jamiewong/code/stackoverflow

$ pwd
/Users/jamiewong/code/stackoverflow

Rerun the last command

From: Run the previous command with sudo @ commandlinefu.com

$ echo hello
hello

$ !!
echo hello
hello

$ ln -s some_script.sh /usr/bin/some_script
ln: /usr/bin/some_script: Permission denied

$ sudo !!
sudo ln -s some_script.sh /usr/bin/some_script

$ ls -l /usr/bin/some_script
lrwxr-xr-x  1 root  wheel  14 30 Jun 00:44 /usr/bin/some_script -> some_script.sh

$ cat /etc/apache2/passenger_pane_vhosts/searchgraph*
<VirtualHost *:80>
    ServerName searchgraph.local
    DocumentRoot "/Users/jamiewong/Code/searchgraph/public"
    RailsEnv development
    <Directory "/Users/jamiewong/Code/searchgraph/public">
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>

$ echo "!!"
echo "cat /etc/apache2/passenger_pane_vhosts/searchgraph*";
cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf

Modify the last command and run it

From: Runs the previous command but replacing @ commandlinefu.com

$ echo hello world
hello world

$ ^world^friends
echo hello friends
hello friends

$ ls *.php
bolding.php shd.php     stream.php

$ ^php^rb
ls *.rb
hms.rb           split_orderby.rb

$ cds ..
-bash: cds: command not found

$ ^s
cd ..

List your last n commands

$ history 10
    522  locate mysqld
    523  sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
    524  sudo /Library/PreferencePanes/MySQL.prefPane/Contents/MacOS/MySQL
    525  sudo mysqld_safe
    526  sudo /usr/local/mysql/bin/mysqld_safe
    527  mysql
    528  history
    529  history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
    530  history
    531  history 10

Edit and run a previous command

From: Edit the last command line in an editor then execute @ commandlinefu.com

This one requires a bit of explanation. The fc command will, by default, open your last command in the console editor you specify (emacs by default).
If you want it to be vim, stick this in your .bash_profile to just run it when you want to use it.

export EDITOR=vim

Then run fc, and it will open up your editor with your last command in it. Save and exit (:wq) to run that command. If you just want to execute it immediately, use fc -s.

$ history 5
    86  cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf 
    87  echo "cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf "
    88  history 10
    89  echo "cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf "
    90  history 5

$ fc -s 87
echo "cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf "
cat /etc/apache2/passenger_pane_vhosts/searchgraph.local.vhost.conf

If you don’t care to look up the history number but happen to remember that it was the command you executed two lines ago, you can use negative history numbers

$ echo hello
hello

$ echo world
world

$ fc -s -2
echo hello
hello

Follow new output to a log

This one comes from my coworkers

$ tail -f ~/code/searchgraph/log/development.log 


Processing HomeController#bp (for 127.0.0.1 at 2010-06-23 22:19:27) [GET]
Rendering home/bp
Completed in 6ms (View: 5, DB: 0) | 200 OK [http://searchgraph.local/bp]


Processing HomeController#bp (for 127.0.0.1 at 2010-06-23 22:20:46) [GET]
Rendering home/bp
Completed in 9ms (View: 7, DB: 0) | 200 OK [http://searchgraph.local/bp]

Then if I go open up searchgraph.local, my console window will get updated on its own

Copying and pasting from files

The pbcopy and pbpaste commands copy and paste from your clipboard.

$ echo test > test.txt

$ pbcopy < test.txt

$ pbpaste
test

And if you want to paste content you have in your clipboard to a file, just redirect, with

$ pbpaste > output.txt

That’s all for now - I’ll write more down as I find them.

If you liked reading this, you should subscribe by email, follow me on Twitter, take a look at other blog posts by me, or if you'd like to chat in a non-recruiting capacity, DM me on Twitter.


Zerø Wind Jamie Wong