Zero Wind – Jamie Wong Inside the mind of a Waterloo Software Engineering student

29Jun/102

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.

In all the examples below, the stuff inside [ ]'s is my current working directory. It's the result of my .bash_profile command

export PS1="[\e]2;Jamie@Local \a\e[32;1m] 
\033[1;32m  [\W]  \033[1;34;40m\033[1;32;0m"

And yes, I have been spending a lot of time on Stack Overflow.

Change to previous working directory

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

  [stackoverflow]  pwd
/Users/jamiewong/code/stackoverflow

[stackoverflow] cd /usr/bin

[bin] pwd /usr/bin

[bin] cd - /Users/jamiewong/code/stackoverflow

[stackoverflow] pwd /Users/jamiewong/code/stackoverflow

Rerun the last command

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

  [stackoverflow]  echo hello
hello

[stackoverflow] !! echo hello hello

  [stackoverflow]  ln -s some_script.sh /usr/bin/some_script
ln: /usr/bin/some_script: Permission denied

[stackoverflow] sudo !! sudo ln -s some_script.sh /usr/bin/some_script

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

  [stackoverflow]  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> 
  [stackoverflow]  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

  [stackoverflow]  echo hello world
hello world

[stackoverflow] ^world^friends echo hello friends hello friends

  [stackoverflow]  ls *.php
bolding.php shd.php     stream.php

[stackoverflow] ^php^rb ls *.rb hms.rb split_orderby.rb

  [stackoverflow]  cds ..
-bash: cds: command not found

[stackoverflow] ^s cd ..

[code]

List your last n commands

  [stackoverflow]  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 - yuck). If you want it to be vim (yay!), 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.
  [stackoverflow]  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

[stackoverflow] 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
  [stackoverflow]  echo hello
hello

[stackoverflow] echo world world

[stackoverflow] fc -s -2 echo hello hello

Follow new output to a log

This one comes from my coworkers

  [stackoverflow]  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.

  [stackoverflow]  echo test > test.txt

[stackoverflow] pbcopy < test.txt

[stackoverflow] 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.

Filed under: Uncategorized 2 Comments
12Dec/092

Manual Sorting

cards

The basis for this post is the following question: what is the fastest algorithm for sorting a deck of cards by hand?

TL:DR advisor - skip to the end of the post for a challenge.

In terms of algorithmic complexity, merge sort and quick sort are two of the fastest widely used sorting algorithms - both running at an average case of O(n log n). But if you've ever sorted a deck of cards, ordering primarily by suit and secondly by number, I doubt you used either of these algorithms. You might unknowingly be doing bucket sort, dividing the cards into 4 "buckets" - one for each suit, then sorting each suit using a different algorithm and joining all the suits together at the end.

If I had a deck of cards in my room right now, I would be inclined to take videos of me sorting them using various different algorithms and comparing the time required. I have a feeling that the fastest algorithm would involve drawing out a 4x13 grid on a big piece of paper with each cell labeled with the exact card that fits there, then running through the deck, placing all the cards on their grid cell and just picking them up in order at the end. Of course, this could be accomplished without the grid but requires a spacial sense which I simply do not possess.

As a followup, I have another question: what if the cards were labeled with a number system you've never seen before? Assume you have some visual reference allowing you to understand the system, but also assume that the number system is not intuitive. Does your approach change? Of course, the fastest approach here depends on what I'm really asking by "fastest". Given enough time and practice, you would be able to become familiar enough with the numeral system to use any approach you would with regular cards or regular decimal numbers. When I say "fastest", I'm asking how you would minimize the time between receiving the cards and the visual reference and the cards being sorted. In terms of computation, the introduction of this numeral system has drastically increased the time for a comparison or enumeration while not affecting the time for movement or swapping at all. The reverse would be to use the standard deck of cards but make them orders of magnitude larger - say 1 meter wide each. In this case a move or a swap is extremely costly, but a comparison is very cheap.

A more realistic scenario in which the cost of comparison is drastically increased comes about when the criteria used for sorting is not an absolute. Example: sort these pictures by aesthetic appeal. Even by yourself this may be a long process, as you second guess your original decision about the relative appeal of a picture. In the world of web 2.0 though, it's a much much longer process. Sorting by crowd-sourced opinion is the basis of many websites, such as bash.org and reddit. Most of these systems work by providing users with the ability or increase or decrease the item's value by small amounts. Is there some better way of ensuring that the articles which will be the most valuable to the readers will show up at the top?

Returning to the point of manual sorting, I have 2 more things to say.

The first is an idea - competitive sorting. I'm sure this sounds nerdy as hell, but I think I'd still find it fairly entertaining. Groups of people would be given sets of objects and told to sort by some criteria, the fastest group being the victor, with penalties dished out for people falsely claiming their list is sorted. The criteria could be size, weight, volume, buoyancy, color saturation, retail price, alcohol content, power consumption or even something as obscure as average salary of a worker for the company manufacturing the product. Given that I'm at Waterloo, I feel that organizing such a competition isn't all that unlikely. Anyone feel like coming up with a plan for making this actually happen?

The second is a challenge. I challenge everyone reading this to make a video of them sorting a deck of 52 cards (no jokers) as fast as they can, then post the video in the comment section. As soon as I actually get a deck of cards, I'll take part in this challenge myself.

Rules.

  1. The video must be all in one take
  2. The deck must be shuffled thoroughly on screen, then fanned towards the camera to demonstrate the randomized order of the cards.
  3. Once the cards have been fanned towards the camera, you can have a maximum of 10 seconds of review time looking through the cards before the sorting starts. All cards must stay in contact and in order during this review time.
  4. The timer starts the second a card is separated from the rest of the deck.
  5. The timer stops when the deck has been reassembled into a single sorted deck.
  6. A deck is considered sorted if the cards are primarily ordered by suit in the order diamonds, clubs, hearts, spades, and secondarily ascending numerically.
  7. You must fan the deck towards the camera after it is sorted.
  8. You are allowed any setup you want before starting to sort (e.g. the grid I talked about above,) provided that the setup does not contain moving parts - no robots.
  9. You must be the only one handling the cards - no team efforts.

That's all. I fully expect to receive no responses to this challenge, but would love to see the things people come up with if someone does try it.

EDIT: Woohoo! Someone attempted! Timothy Armstrong Sorting

Also, the claimed record according to this page is 36.1 seconds.

4Nov/090

ReBirth

This post marks the birth of yet another website I will inevitably use for about a year then run out of time and stop updating. Hopefully this one won't get eaten by a worm like the last one though. Stupid obscure javascript vulnerability.

The virus that owned the old site (along with every other index.* on my webspace) was a variant of the Gumblar.cn virus http://blog.unmaskparasites.com/2009/05/07/gumblar-cn-exploit-12-facts-about-this-injected-script/

It infected my computer just by visiting a site, found my stored ftp credentials from FileZilla (teaches me not to store my passwords), logged into my webspace and proceeded to modify any index.* file. Very evil, very clever virus. Right on that tipping point between me being impressed and me being horrified.