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
add comment

5 Answers 5

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
add comment

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
add comment

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
add comment

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
add comment

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
add comment

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.