Tip: Move up fast in directory structure in Linux shell



Categories: Linux

If you work regularly in Linux shells and are navigating through the directories, you may find yourself deep in some directory structure like /home/bla/test/other/and/another/dir and you want to come up to other. The two obvious commands here are /home/bla/test/other/and/another/dir and cd ../../../. Sometimes you might be deeper in the shit and if you are regular into this, you might it annoying some times.

Today I came across a tip while browsing through this Quora Answer, which explains how can we make our life easier in this case.

If you are using Bash, put below code in your .bash_profile file. And then you can use up other directly to come up to other directory in above example.

function up { 
    cd `expr "$PWD" : "^\(.*$1[^/]*\)"`

Since, CSH do not support the functions, I converted it to alias. You can put this in your .cshrc and use it as <span class=“text”>up other</span>

alias up 'cd `expr "$PWD" : "\(.*\!*[^/]*\)"`'

Happy shell scripting.