Developer Aspirations

YAPB - Yet Another Programming Blog



April 2014

Upgrade Your Command-Line - Part 2: super cd ..

by Colin Miller, on command-line, bash

In the first part of this series I talked about bashmarks for quickly navigating using a bookmark like system. In this part, I'd like to introduce another enhancement for speeding up navigation: upgrading the cd .. command.

As you know, unix-like filesystems have 2 virtual directory markers in each directory. The . directory represents the current directory. The .. directory represents the parent directory. I find that often when I'm navigating deep into the filesystem and I want to back out a certain amount, I end up typing either:

cd ../../../../


cd ..
cd ..
cd ..
cd ..

That's a lot of typing! If you add the following snippet to your ~/.bashrc (all one line)

cd() { if [[ "$1" =~ ^\.\.+$ ]];then local a dir;a=${#1};while [ $a -ne 1 ];do dir=${dir}"../";((a--));done;builtin cd $dir;else builtin cd "$@";fi ;}

Then the cd command will be able to accept additional dots for moving into grandparent directories. So now what was cd ../.. becomes cd .... You'll have to source your ~/.bashrc or restart your shell (or paste that snippet to your command-line) to get it working in your current session.

Each additional dot beyond the first two go up one more directory. So if you want to go up 2 directories, you would use cd .... If you want to go up 5 directories, you would use cd ....... Less typing, faster navigation.

Next up: part 3: tmux

comments powered by Disqus