Home - Archive - About Me - Feed

Command line one-liners


I attended the SoCraTes UK unconference. We had a space for lightning talks and I did a presentation about command line one-liners.

I love Unix and I was talking about terminal commands, so I used Terminal Keynote – a hack for terminal-based talks.

Contribute to the list with your own commands in the command line one-liners repository.

Terminal Keynote

Command line one-liners

Run the last command

$ !!

Run the last command as root

$ sudo !!

Create a script of the last executed command

$ echo "!!" > script.sh

Reuse all parameter of the previous command line

$ echo cd .
$ !*

Run the last command with some argument

$ echo a b c d e
$ echo !!:2
$ echo !!:3-$

Insert the last argument of the previous command

$ cp script.sh /usr/bin/
$ cd <ESC> .

Runs previous command but replacing

$ echo no typos
$ ^typos^errors

Escape any command aliases

$ alias ls="ls -a"
$ \ls

Quickly rename a file

$ mv filename.{old,new}
$ mv filename.{png,jpg}

Create a quick back-up copy of a file

$ cp file.txt{,.bak}

Run a command from the history

$ history
 1225  ls -l
 1226  git status
 1227  history
$ !-3
$ !1225

Search the history for the most recent command beginning with ‘text’

$ !text

List of commands you use most often

$ history | awk '{print $2}' | sort | uniq -c | sort -rn | head

Execute a command without saving it in the history

$ <space>command

Make directory including intermediate directories

$ mkdir -p a/long/directory/path

Create a directory and change into it

$ mkdir dir && cd $_

Change to the previous working directory

$ cd -

Jump to a directory. Execute a command. Jump back to current directory

$ (cd /tmp && ls)

Create simple text file from command line

$ cat > file.txt
{your text here}
{your text here}

Empty a file

$ > file.txt

Show PATH in a human-readable way

$ echo $PATH | tr ':' '\n'
$ tr ':' '\n' <<< $PATH

Make ‘less’ behave like ‘tail -f’

$ less +F somelogfile

Redirect standard input to a file. Print it to standard output

$ command | tee file.txt | less

┌─────────┐  ┌─────────┐  ┌─────────┐
│ command │─▸│   tee   │─▸│ stdout  │
└─────────┘  └────┬────┘  └─────────┘
            │   file    │

Search for a string inside all files in the current directory

$ grep -RnsI --color=auto <pattern> *

Beyond grep

_   /|
  U    ack!

$ ack <pattern>

Recursively remove all empty directories

$ find . -type d -empty -delete

Count your commits

$ git shortlog -sn

Serve current directory tree at http://$HOSTNAME:8000/

$ python -m SimpleHTTPServer

Share a file between two computers

$ nc -l 5566 > data-dump.sql
$ nc <his-ip-address> 5566 < data-dump.sql

Download an entire website

$ wget -m http://website.com

Clear the terminal screen


Salvage a borked terminal

$ reset

Close shell keeping all subprocess running

$ disown -a && exit

Run a command immune to hangups

$ nohup command &

Attach screen over ssh

$ ssh user@host -t screen -r

Compare a remote file with a local file

$ ssh user@host cat /path/to/remotefile | diff /path/to/localfile -

Get your public IP address

$ curl ifconfig.me

Set audible alarm when an IP address comes online

$ ping -a IP_address

List programs with open ports and connections

$ lsof -i

Currently mounted filesystems in nice layout

$ mount | column -t

Display free disk space

$ df -h

Display disk usage statistics for the current directory

$ du -sh *

Display 10 biggest files/folders for the current directory

$ du -s * | sort -nr | head

Execute a command at a given time

$ echo "ls -l" | at midnight

Simple stopwatch

$ time read

Put a console clock in top right corner

$ while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &

Display the top ten running processes. (Sorted by memory usage)

$ ps aux | sort -nk +4 | tail

Kill all Ruby processes

$ ps aux | grep ruby | awk '{ print $2 }' | xargs kill -9
$ ps aux | awk '/ruby/ && ! /awk/ { system("kill -9 "$2) }'

32 bits or 64 bits?

$ getconf LONG_BIT

Displays a calendar

$ cal 12 1984

What day is today?

$ cal | sed "s/.*/ & /;s/ $(date +%d) / [] /"
$ cal | sed "s/.*/ & /;s/ $(date +%d) / $(printf '\e[0;31m[]\e[0m') /"

Show File System Hierarchy

$ man hier

Quick access to the ascii table

$ man ascii

Russian Roulette in Bash

$ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live"

Watch Star Wars via telnet

$ telnet towel.blinkenlights.nl



In recent months, the fun happens in private repositories. The last deployed project is iShows.

We love TV shows, we love programming, we love design and we believe that we can create the best TV show tracking app on iOS. Users seems to enjoy the application and it had so good reviews.


We wanted to provide an image-based UI and one of the strengths of the app is the customization of the images. All the credits go to Luis Recuenco and Luis Herrero for the iOS development and UI/UX design.

I developed an image server that provides all the images and is responsible to make the resize and cropping, so the clients download the images with the correct size.

iShows is in the App Store. Enjoy!

Markdown Resume


A few weeks ago I needed to build my curriculum vitae. When was the last time that you composed your cv? The last time I had to build it, I used a completely different stack. I remember having used Microsoft Office Word.

Now my options are an online editor, LaTeX or Markdown. I love to write in Markdown and I like to create my own tools so I decided to craft a tool to do the job.


Biteydown is Ruby application that turn a simple Markdown document into a resume in HTML or PDF file.


bundle install


Usage: biteydown [options] file
        --html                       Create HTML file
        --pdf                        Create PDF file
    -h, --help                       Display help

$ bin/biteydown --html --pdf example/curriculum.md


You can customize the look and feel of text and headings in your document using the style/style.css file. First is transformed the Markdown document to HTML file and then apply the style. It’s a little bit hacky, but works fine.

Markdown is a lightweight markup language designed as an easy-to-read, easy-to-write plain text format and then convert it to structurally valid HTML; therefore, CSS selectors can be used to customize the curriculum.


CSS works by associating rules with HTML elements. In this case, there are a little group of selectors that can be used: h1-h6, blockquote, a, li, code, strong, em, img. You cannot use id or class selectors to create rules that apply to elements.

Managing Notebooks


There are a lot of tools to organize and get work done, some are more focused on task management like Do or Wunderlist, and others are more focused on project management like AgileZen or Trello.

These are great tools, but I prefer notebooks. I thought to write about notebooks hacks, but there is already a monster collection of Moleskine tips, tricks and hacks.

Mike Rohde Notebook

Bill Westerman Notebook

I prefer notebooks over other devices and media. It’s the same feelings with books and e-readers. I love the feel of paper and provide a better user experience for me. In many ways it’s more versatile than software because I can organize my daily tasks, communicate ideas through drawing, sketchnotes, create lists, project planning, journal, quotes, random thoughts…



I start to hack on an idea I had last week. The idea is to display statistics about version control systems in a Unix shell and I also want to improve my command line fu.

Before starting, I remembered spark, an awesome project that generates sparklines for a set of data, so I interested in trying out an alternative Groovy implementation and have fun.

▁▂▃▅▂▇ in your shell. Groovy flavoured!

My project is sparky. It’s a Groovy script that lets you graph tiny sparkline graphs from your command line.

sparky takes a list of numbers (comma-delimited, spaces, whatever you’d like) and then prints a sparkline out of it. It’s designed to be used in conjunction with other scripts that can output in that format.


$ sparky 1 5 22 13 53

$ sparky 0,30,55,80,33,150

$ echo 9 13 5 17 1 | sparky

Cooler usage

There’s a lot of stuff you can do.

Number of commits to the github/groovy-core Git repository, by author:

$ git shortlog -s |
> awk '{print $1}' |
> sparky

Code visualization. The number of characters of sparky itself, by line, ignoring empty lines:

$ awk '{ print length($0) }' sparky |
> grep -v 0 |
> sparky

Try it!

Ruby Version Manager


I’m a Groovy developer. As you might know, I love to learn and play with other programming languages.

This time, it’s the turn of the Ruby programming language. Ruby has a great ecosystem, there are a plenty of projects, libraries and tools. In fact, this blog is powered by toto and right now I’m creating a new static site with Jekyll.

To develop these projects, I need to use different Ruby versions and gems. The established leader in the Ruby version management scene is RVM and rbenv is an alternative.


Ruby Version Manager (RVM) is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.

I have taken some notes that I think you will find helpful if you are starting with RVM.

Install RVM:

curl -L get.rvm.io | bash -s stable

Display a list of all “known” rubies:

rvm list known

Install Ruby:

rvm install 1.9.3

I had issues with zlib and openssl, I reinstalled Ruby including these packages:

rvm pkg install zlib openssl
rvm reinstall 1.9.3 --with-zlib-dir=$rvm_path/usr --with-openssl-dir=$rvm_path/usr

Switch beetween Ruby versions:

rvm 1.9.3
rvm system

The project .rvmrc files is intended to be used to setup your project’s Ruby environment when you switch to the project root directory.

Create a project specific .rvmrc file:

rvm --rvmrc --create 1.9.3@project

Closure Design Patterns


Design Patterns

These days I’m learning design patterns. There are a lots of documentation about software design patterns, but I’m interesting in closure design patterns.*

Many patterns imply object-orientation, so may not be as applicable in dynamic languages. Peter Norvig demonstrates that 16 out of 23 patterns in the Design Patterns book are simplified or eliminated, Design Patterns in Dynamic Languages.

I’ve found an interesting presentation of Venkat Subramaniam about Design Patterns in Java and Groovy and another presentation of Neal Ford about Design Patterns in Dynamic Languages. Here, I extract some patterns of these presentations that involve closures and add others patterns based on my own experience.

* Groovy makes no such distinction between closures or anonymous functions. I’m really trying to get at is how we can use tools such as first-class functions, lambdas and closures when implementing design patterns.

Closure Design Patterns

Execute Around Method

A pair of operation that needs to be performed before and after operations.

def operations(closure) {
    println "Open"
    println "Close"

operations { println "Operation" }

===> Open
===> Operation
===> Close

Pluggable Behavior

Specify the behavior of an object at runtime.

def selectValues(number, closure) {
    def list = []
    1.upto(number) {
        if (closure(it)) list << it
    return list

assert [2, 4, 6, 8, 10] == selectValues(10) { it % 2 == 0 }  // even
assert [1, 3, 5, 7, 9]  == selectValues(10) { it % 2 != 0 }  // odd

Iterator Pattern

Allows sequential access to the elements.

def listNumbers(closure) {
    (0..3).each { closure it }

listNumbers {
    if (it < 2) println "$it is a little number"
    else println "$it is a big number"

===> 0 is a little number
===> 1 is a little number
===> 2 is a big number
===> 3 is a big number

Dynamical Conditional Execution

Create and execute a conditional operation.

def greet(user, successClosure, failClosure) {
    if (isAdmin(user)) successClosure()
    else failClosure()

greet(user, { println "Hi Admin!" }, { println "Hello User" })

Template Method Pattern

Define common algorithm steps (getting a customer) and customizations (passed as a closure).

def withCustomer (id, closure) {
    def customer = getCustomer(id)

withCustomer(1234) { customer ->
    println "Found customer $customer.name"

Loan Pattern

Ensures that a resource is deterministically disposed of once it goes out of scope.

def withListOfWordsForEachLine(file, closure) {
    def reader = file.newReader()
    try {
        reader.splitEachLine(' ', closure)
    } finally {

withListOfWordsForEachLine(file) { wordList ->
    println wordList

Command Design Pattern

Encapsulate all the information needed to call a method at a later time.

def count = 0
def commands = []

1.upto(10) {
    commands.add { count++ }

println "count is initially ${count}"
commands.each { cmd ->
println "did all commands, count is ${count}"

===> count is initially 0
===> did all commands, count is 10

Strategy Pattern

Define a family of interchangeable algorithms.

calcMult = { n, m -> n * m }
calcAdds = { n, m ->
    def result = 0
    n.times { result += m }
    return result

def calcStrategies = [calcMult, calcAdds]
calcStrategies.each { calc ->
    assert 10 == calc(5, 2)

Factory Pattern

Abstract the object creation process (currying as a function factory).

def adder = { x, y -> x + y }
def incrementer = adder.curry(1)

assert 5 == incrementer(4)

Method Combination

Build a method from components.

def sum = { Collection collection -> collection.sum() }
def first2 = { Collection collection -> collection.take(2) }
def take2andAdd = first2 >> sum

assert 3 == take2andAdd([1, 2, 3, 4, 5])

Polyglot Programming


Polyglot Programming

How many programming languages do you know?

Most programmers know several languages. I, as web developer, work on a daily basis with Groovy, SQL, Bash Scripting, HTML, CSS, JavaScript…

Learning different languages allows us to solve problems with the most appropriate language and to explore new paths of thinking about computational problems.

Neal Ford coined the term polyglot programming, to refer about this topic.

I think it’s important in this day and age of polyglot programming to understand a variety of different languages, as they are the design tools we use to craft software. Just like regular engineers must understand the physical properties of different materials, we should understand the capabilities, strengths, and weaknesses of different languages. And when to apply them.

Neal Ford

Polyglot JVM

Java has long been known simply as a programming language. Today, when thinking in Java we refer also a robust and mature development platform. Currently, the Java Virtual Machine (JVM) supports over 200 different programming languages.

These days I have been exploring different languages on the JVM: Java, Groovy, Scala, Clojure… I shared my experiences with others developers at Codemotion 2012.

Hope you enjoy the slides.

Create your own Groovy type conversion


Type conversion the standard way

Type conversion or casting is a programming language method for changing an object’s data type into another.

I’m sure you are familiar with this code that converts a String to an Integer.

def number = (Integer)'1'
def number = '1'.toInteger()
def number = '1' as Integer

If I want to convert the type of my own objects, I need to create a method to achieve this goal. I copy object properties to another object in a generic way; if a property exists on target object, I copy it from the source object.

class User {
    String name
    String city
    Integer age

    def toAdminUser() {
        def adminUser = new AdminUser()
        copyProperties(this, adminUser)
        return adminUser

    def copyProperties(source, target) {
        source.properties.each { key, value ->
            if (target.hasProperty(key) && !(key in ['class', 'metaClass'])) {
                target[key] = value

class AdminUser {
    String name
    String city
    Integer age

Now I can do something like this:

adminUser = user.toAdminUser()

Type conversion the fancy way

Great, but I want to use this fancy way to coerce one type to another:

adminUser = user as AdminUser

Simple, Groovy supports operator overloading and creating your own type conversion is really easy: we can override the asType() method.

class User {
    String name
    String city
    Integer age

    Object asType(Class clazz) {
        if (clazz == AdminUser) {
            def adminUser = new AdminUser()
            copyProperties(this, adminUser)
            return adminUser
        else {

    def copyProperties(source, target) {
        source.properties.each { key, value ->
            if (target.hasProperty(key) && !(key in ['class', 'metaClass'])) {
                target[key] = value

GNU Screen


GNU Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. With this tool, you can keep a process running after disconnecting an SSH session. Kudos to Diego Toharia and his blog post Screen en máquinas remotas.

Basic usage

Create a new screen session:

$ screen -S sessionname

Resumes a detached screen session:

$ screen -r sessionname

List all of the screen sessions:

$ screen -ls

Kill a session:

$ screen -S sessionname -X quit

Close all screen sessions:

$ killall screen

Useful key bindings:

Ctrl + a, d Detach screen from this terminal
Ctrl + a, c Create a new window
Ctrl + a, space Switch to the next window
Ctrl + a, backspace Switch to the previous window
Ctrl + a, A Rename current window
Ctrl + a, " List of all windows for selection
Ctrl + a, k Destroy current window
Ctrl + a, ? Show key bindings

Multiple shells open in the same terminal:

Ctrl + a, | Split vertically
Ctrl + a, S Split horizontally
Ctrl + a, tab Switch the input focus to the next region
Ctrl + a, X Kill the current region
Ctrl + a, Q Delete all regions but the current one

A worry-free session

$ ssh me@server.com
$ screen -S sessionname
$ start something really important

Disconnect from the server (panic at other times without screen).

$ ssh me@server.com
$ screen -r sessionname

Everything is fine, keep working.

Automate your job

After disconnection, you can log into the remote machine and reattach the session in a single step:

$ ssh me@server.com -t "screen -r sessionname"

How can we improve this? autossh is the answer.

autossh, automatically restarts an SSH session and reattaches a session transparently:

$ ssh me@server.com
$ screen -S sessionname
$ Ctrl + a, d
$ exit
$ autossh me@server.com -t "screen -r sessionname"

In fact, autossh include a script called rscreen for perpetual sessions.

OK, it works. But it’s not so great because first you need to connect to the server, create a new screen session, detach the screen, exit from the server and finally connect it again with autossh.

What can we do? You can use screen -R to reattach a session or even create it first. Finally we solve all problems:

$ autossh me@server.com -t "screen -R sessionname"

Want to read more? See the Archive >>