7

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?

8 Answers 8

8

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.

1
  • 1
    Similar function here, but I like yours better cause it automatically handles OLDPWD.
    – wjandrea
    Jan 11, 2020 at 2:10
5

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 ../../../../../../../'
2
  • i don't like alias solution but you taught me pushd and popd which are really useful Mar 7, 2012 at 19:40
  • isn't the solution useful? why was it downvoted??
    – Mallik
    Mar 10, 2012 at 9:23
3

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
}
2
  • 2
    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, 2012 at 19:43
  • This code doesn't pass ShellCheck and the cd-ing is more complicated than it needs to be, plus the style is bad. Use geirha's solution instead.
    – wjandrea
    Jan 11, 2020 at 2:06
3

Be concise.

alias c1='cd ../'
alias c2='c1; c1'
alias c3='c2; c1'
alias c4='c3; c1'
alias c5='c4; c1'
2

Coming from the Windows world, Alt + Up Arrow navigates to the parent directory in Windows Explorer. So I made something like this in ~/.inputrc:

"\33\33[A": "cd ..\n"

then pressing Alt + Up Arrow moves to the parent directory in the terminal. You have to press multiple times of course to move higher, but I have found it to be very fast. Also you can change the shortcut to your liking.

3
2

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 directory.

1
  • 1
    .bashrc, not .bash_profile
    – geirha
    Mar 7, 2012 at 19:37
0
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'
-1

Using aliases with dots as names:

alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'
alias ......='cd ../../../../../'
2
  • Somehow I affraid this might break in interesting ways. Have you tried this solution on your own ubuntu?
    – MadMike
    Oct 21, 2021 at 14:21
  • 1
    Works for me in ZSH on Debian. I am also open to scenarios where it will break. :) The ones who want to be on the secure side may prepend the aliases with a string or a character e.g. "c..", "c..." and so on.
    – nvd
    Oct 21, 2021 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .