Charlie Harvey

Tip: from the commandline

This is a pretty minimal tip. The other day I was looking for a way to shorten URLs from the commandline. Now the API expects you to authenticate before doing any shortening (I’ve not pulled apart the web ui to see if its possible without). The URL shortener has no such restrictions, so let's see some commandline URL shortening.

Using wget

We start off using wget, a lovely tool which makes requesting web pages easy. I use the -q option to quiten it down and -O - to make it dump the output to stdout. Simple, classic. Note the trailing echo statement, which is for formatting only, so we get a newline after our URL. $ wget -q -O - "";echo $

Using curl

The other popular commandline http requesting tool is curl. We use its silent mode with -s. The call is even simpler than with wget. $ curl -s ""

Using lynx

Lynx is a non-graphical web browser and lets you run in "dump" mode where it will output a simplified text version of an html page to STDOUT. It uses the Java-esque single hyphen for long commandline arguments convention. It also indents the output a bit. Running the output through tr is left as an excercise for the reader. $ lynx -dump "" $

The bulletproof way: with perl

I didn't mention that I have been assuming that your URL is already URI escaped (%20s for spaces and similar, see RFC 2396 for the gory details). We can make one that escapes your URI and fetches the short URL with perl. Here goes. $ perl -MLWP::Simple -MURI::Escape -e 'print get("" . uri_escape("") ) . "\n";' $ I use the LWP::Simple module to run the API call and URI::Escape to, umm, escape the URI. The -e switch tells perl to evaluate the bit between the 's.


  • Be respectful. You may want to read the comment guidelines before posting.
  • You can use Markdown syntax to format your comments. You can only use level 5 and 6 headings.
  • You can add class="your language" to code blocks to help highlight.js highlight them correctly.

Privacy note: This form will forward your IP address, user agent and referrer to the Akismet, StopForumSpam and Botscout spam filtering services. I don’t log these details. Those services will. I do log everything you type into the form. Full privacy statement.