How to quickly move into and from deeply nested directories through CLI?

There is pushd and popd:

pushd /home/thina/teams/td/tech/app/release/apks
# current directory now /home/thina/teams/td/tech/app/release/apks
popd
# current directory now what it was before pushd command

Try help pushd and help popd for more options. There is no man page, because pushd and popd are bash built-in commands.


In addition to the very good answers already provided, here are some tips on using cd effectively.

  • cd - will take you back to the last directory you were in.
  • cd ../../.. will take you up 3 levels at once, you can use the .. notation chained together to 'move up' as many directories as you like.
  • If you're not sure how many times you wish to move up, use cd .., then use bash history by pressing up on the arrow key to use the command again.
  • Use ~ to stand in for the current users home directory, if you're logged in as the user thina, cd ~/teams, will take you to /home/thina/teams
  • Use Bash auto-completion for paths, the tab key will complete a section of a path in the cd command, if you type part of a path segment followed by Tab, that segment will be completed if there's no other valid choice. For instance, if you had typed cd /home/thina/teams/td/t then pressed Tab, the word tech would be filled in for you, so long as there were no other files or directories in the td directory that started with the letter t.

Using these tips together can make traversing directories with cd far less painful.


To go up in the tree several levels at a time, you can use the following function (thanks to muru for the enhanced version):

up ()
{
    local old="$PWD"
    for i in $(seq "${1:-1}"); do
        cd ..
    done
    OLDPWD="$old"
}

Then you can do:

$ pwd
/home/thina/teams/td/tech/app/release/apks
$ up 5
cd'ing into /home/thina/teams

Additionally:

  • calling up without an argument is equivalent to cd .. due to ${1:-1} which substitutes $1 when set and 1 otherwise
  • setting OLDPWD after the last cd .. aims at preserving the usual cd - behavior.