Ever wanted to go up a few directories when using the bash terminal? You probably had to do something like this:
cd .. (enter)
cd .. (enter)
cd .. (enter)
…until the end of time.
Worry not, for gone are those days. I was just browsing through some stackoverflow questions and ran into these extremely useful bashrc functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#functions for going up a certain number of directories and back function up( ) { LIMIT=$1 P=$PWD for ((i=1; i <= LIMIT; i++)) do P=$P/.. done cd $P export MPWD=$P } function back( ) { LIMIT=$1 P=$MPWD for ((i=1; i <= LIMIT; i++)) do P=${P%/..} done cd $P export MPWD=$P } |
Now, all you have to do to go up N directories is type in up N
, hit enter, and like magic, you’re up those many directories.
Here’s an example
Let’s say I’m here:

I want to go up 3 directories. Normally, this would involve typing in cd .. (enter)
three times. Now, all I have to do is type up 3
and I am there, like so:

Now, say I want to go back up 2 directories. All I have to do is type back 2
, and I’m there.

So, how do you install these neat little commands? Here’s a step by step guide:
- Open up your terminal with
Ctrl + Alt + T
. - If you’re not already there, go to your home directory with
cd (enter)
. - Load up your .bashrc file in your favourite text editor. For me, this was
gvim .bashrc
. - Add these functions to your .bashrc file, save, and quit.

5. Go back to your home directory and source your bashrc file like so: source .bashrc
6. You’re done! Enjoy your new bash commands. 🙂
If you like these nifty little commands, please leave a comment below.
wow. this is life changing sir. thank you for the awesomeness.
As promised, hehe. Thanks. 🙂
try binding keyboard shortcuts to it, its better than typing 😀
here is my conf.. for bash
https://github.com/rhoit/my-config/blob/master/shell/inputrc
Haha, this is even better. Thanks for the suggestion, dai.
in zsh, you have handy aliases such as
cd...
(with three dots) meaning go up two directories.cd...
to go up three and so on pre-configured for you. If you need to go up more than that, then you might wanna look at this plugin: https://github.com/rupa/zIt’s quite magical, I tell you!
And if you’re not using zsh by now, you should! It’s like bash, but on steroids. 🙂