Go Up As Much Directory Levels As You Need – Easily

This neat and brilliant script by shell-fu allows you to go up as many directory levels as you want.

For example, instead of typing

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

You’ll be able to type the following and get the same functionality:

cd .. 4

Here is the bash function which does the magic:


1
2
3
4
5
6
7
8
9
10
# .. - Does a 'cd ..'
# .. 3 - Does a 'cd ../../..'
#
function .. (){
  local arg=${1:-1};
  while [ $arg -gt 0 ]; do
    cd .. >&/dev/null;
    arg=$(($arg - 1));
  done
}

In order to use it, add the above code to your ~/.bashrc file (or to a new file, and source the file in your bashrc).

Now open a terminal and try it.

Original post is here.

Enjoy ;)

If you enjoyed this post, make sure you subscribe to my RSS feed!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

No related posts.

Tags: , , ,

Saturday, March 7th, 2009 Bash

3 Comments to Go Up As Much Directory Levels As You Need – Easily

  • John says:

    I find that aliasing cd … to cd ../.. is a great way to accomplish something similar as well. I also alias cd.. to cd .. and cd… to cd… – this is a hangover of mine from using 4dos many years ago.

  • Amir Watad says:

    Hi John,
    Thanks for the comment.
    Yep, I also had similar aliased, till I found the above function at shell-fu. I think it’s much easier to type .. 5 than cd …..
    And, this function if more general, number of levels you can go up is not limited here. Don’t you agree?

  • John says:

    For sure, my way is a hack. The way you use it is true shell-fu. :)

  • Leave a Reply

    my email
    my photo
    Hi,
    My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and a BSc. in electrical engineering from The Electrical Engineering school , Technion , Israel.
    I'm a Verification Engineer in Mellanox Technologies Ltd.
    I love Linux, the Command Line and the OpenSource Community.
    I used to write Poems (Arabic) when I was able to find time for this.
    March 2009
    S M T W T F S
    « Feb   May »
    1234567
    891011121314
    15161718192021
    22232425262728
    293031  
    SEO Powered by Platinum SEO from Techblissonline

    Twitter links powered by Tweet This v1.7.4, a WordPress plugin for Twitter.