Two Useful bashrc Functions that will Change Your Life

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.

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:

This is where I am initially.
This is where I am initially.

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:

Up 3
Up 3

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

Back 2
Back 2

So, how do you install these neat little commands? Here’s a step by step guide:

  1. Open up your terminal with  Ctrl + Alt + T.
  2. If you’re not already there, go to your home directory with  cd (enter).
  3. Load up your .bashrc file in your favourite text editor. For me, this was  gvim .bashrc.
  4. Add these functions to your .bashrc file, save, and quit.
Functions added to bashrc
Functions added to bashrc

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.

Ashish Acharya
I am an experienced full-stack web developer, specializing in Python and JavaScript, with a penchant for efficient, readable code. I have worked in, as well as led, software development teams and am comfortable within a modern Scrum/Agile-based team setting. I am passionate about automation, GIS, and data science.

5 Comments

  1. 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/z

    It’s quite magical, I tell you!

    And if you’re not using zsh by now, you should! It’s like bash, but on steroids. 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *