Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

if I'm in a very deep directory a/b/c/d/e/f/g/h/i/j and want to come back a/b/c, I have to use ../../../../../../../.

Is there command I can pass through a number, e.g. cd up 7, to speed this operation up?

share|improve this question

6 Answers 6

up vote 3 down vote accepted

Creating an alias would work as a temporary solution, however if you want something more permanent that doesn't confine you to your presets I suggest writing a function to do this and including it in your .bashrc file.

source

# Go up directory tree X number of directories
function up() {
        COUNTER="$@";
    # default $COUNTER to 1 if it isn't already set
if [[ -z $COUNTER ]]; then
    COUNTER=1
fi
# make sure $COUNTER is a number
if [ $COUNTER -eq $COUNTER 2> /dev/null ]; then
    nwd=`pwd` # Set new working directory (nwd) to current directory
    # Loop $nwd up directory tree one at a time
    until [[ $COUNTER -lt 1 ]]; do
        nwd=`dirname $nwd`
        let COUNTER-=1
    done
    cd $nwd # change directories to the new working directory
else
    # print usage and return error
    echo "usage: up [NUMBER]"
    return 1
fi
}
share|improve this answer
1  
You should avoid using all uppercase variable names to avoid accidentally overriding special shell variables or environment variables. Also a good idea to make variables local to the function. –  geirha Mar 7 '12 at 19:43

If you are toggling between 2 directories, you can use cd - to switch between both.

If you want to bookmark a few directories, that you would probably cd to often, use pushd and popd -> google for more information.

Or, if you know you have to cd to 7th grand parent very often, you could create an alias, like

alias cd7up='cd ../../../../../../../'

share|improve this answer
    
i don't like alias solution but you taught me pushd and popd which are really useful –  Lai Yu-Hsuan Mar 7 '12 at 19:40
    
isn't the solution useful? why was it downvoted?? –  Mallik Mar 10 '12 at 9:23

You can create aliases to do the work.

alias cd..2="cd ../.."
alias cd..3="cd ../../.."
alias cd..4="cd ../../../.."
alias cd..5="cd ../../../../.."

And then you can just type cd..5 to go up 5 levels.
To make those aliases available in future logins, you can add the above to the .bash_profile file in your home dir.

share|improve this answer
    
.bashrc, not .bash_profile –  geirha Mar 7 '12 at 19:37

You could write a function like this:

up() {
    local path i
    for (( i=0; i < $1; i++ )); do
        path+=../
    done
    cd "$path"
}

Put that in your ~/.bashrc, then you can run e.g. up 7 to go up 7 directories. You could override cd to allow cd up 7 too, but just making a new command is shorter and less hassle.

share|improve this answer

Be concise.

alias c1='cd ../'
alias c2='c1; c1'
alias c3='c2; c1'
alias c4='c3; c1'
alias c5='c4; c1'
share|improve this answer
function cdl() {

        local arguments;
        local level_string;
        local counter=1;
        # first argument is how many levels you wish to traverse
        local level=$1; 

        # grab any argument after the initial levels you wish to traverse
        for var in "$@"; do
                if [ $counter -gt 1 ]; then
                        arguments="$arguments/$var";
                fi

                counter+=1;
        done

        # build string based on how many levels you want to go up
        if [ $level -gt 1 ]; then
                counter=1;

                while [ $counter -le $level ]; do
                        level_string="../$level_string";
                        let counter+=1;
                done
        fi

        # execute command
        cd $level_string$arguments
}

# Example:
#-----------------
# /usr/local/src/test/directory/blah> cdl 3 i want to be here

or

# /usr/local/src/test/directory/blah> cdl 3 i/want/to/be/here 

result:

# /usr/local/src/i/want/to/be/here> 
#-----------------

Of course I also add aliases in my .bashrc file using the above function

alias cd2='cdl 2'
alias cd3='cdl 3'
alias cd4='cdl 4'
alias cd5='cdl 5'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.