You Need A ‘Go’ Command

If you study your command line history, you’ll probably notice that you often do something like this.

cd somedirectory/
ls

This is incredibly inefficient. Why write two separate commands just to change your directory and list its contents? You need a ‘go’ function which does both.

In your .bash_profile file (usually located in your home directory), add these lines.

go () {
 cd "$1";
 ls;
}

Et voila! You now have a ‘Go’ function. Its usage looks a little bit like this.

Screen Shot 2013-06-18 at 12.57.28

About Matthew Hughes

Matthew Hughes is a software developer, student and freelance writer from Liverpool, England. He is seldom found without a cup of strong black coffee in his hand and absolutely adores his Macbook Pro and his camera. You should follow him at @matthewhughes.

8 comments

  1. If you wrap the $1 in quote marks, your go() function will work for paths with spaces in :-)

    go () { cd “$1″; ls; }

  2. I would definitely call it something else. Used to be users of the go language had to memorize 8g, 8l for compiler and linker and other tools, but now (since 1.0) they are all wrapped into ‘go’ command, dozens of verbs along with go build and go run to save you from using makefiles and even compiling at all.

    Nice little bit of insight, though!

  3. I should hope that somebody who knows how to use the “go” toolkit from the command line also knows how to pick another name when confronted with this dilemma ^_^

  4. Personally, I prefer to keep the original name of the command. On zsh I use the function:

    cd() {
    builtin cd $@; ls
    }

  5. I prefer z over cd: https://github.com/rupa/z

    “Tracks your most used directories, based on ‘frecency’.
    After a short learning phase, z will take you to the most ‘frecent’
    directory that matches ALL of the regexes given on the command line.”

    So if I’m going to ~/Dropbox/notes/ all the time, it notices, and

    $ z note

    takes me to the right place. You could mix this in with your ‘go’ concept and make it even more powerful.

  6. You probably know this already, but I’d just like to point out you can also add an alias for that kind of thing in .bash_aliases

    alias note='cd ~/Dropbox/notes/'

    Back on Windows I used to use stuff like alias d:='cd /cygdrive/d'

    Some others I like are

    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'

    and

    alias ls='ls -hF --color=tty'
    alias dir='ls --color=auto --format=vertical'
    alias vdir='ls --color=auto --format=long'

    There are many more ideas contained over here: http://gnome-look.org/content/show.php?action=content&content=129746

Leave a Reply