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

We know that using cd <directory> will open the directory, if it exists!

Also, cd .. will take us up one level from the current working directory.

But, when we have a nested dir with longer path like /home/thina/teams/td/tech/app/release/apks, it is tiring to just go into the folder apks and also it is hard to get back to another node, say here /home/thina/teams.

Is there any way to skip the typing of cd and cd ..? I'm using Ubuntu 16.04.


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.

For long directory names, use variables with full path. For example,

APKS="/home/thina/teams/td/tech/app/release/apks"

Then you can do just cd "$APKS"

As for going up x number of directories, I have this function defined in my .bashrc

goup() # go up x number of dirs
{
  num=$1
  while [ $num -ne 0  ];do
    cd ..
    num=$( expr $num - 1   )
  done
}

To return to /home/thina/teams from apks you would do

goup  6

Here's an example of usage:

$> pwd
/sys/class/backlight/intel_backlight
$> goup 3
$> pwd
/sys
$> 

Another small function that i came up with, but never used as much is bookmark function.

Here's how it works: it saves your current folder to some file, and then you can cd to a specific directory based on the line number in that file. Example:

$> cd /etc/lightdm
$> bookmark
$> cat ~/.dirsbookmarks                                                                                                  
/home/xieerqi
/sys/class/backlight
/etc/lightdm
$> cd $( awk 'NR==2' ~/.dirsbookmarks  )                                                                                 
$> pwd
/sys/class/backlight

And here is the function itself:

bookmark()
{ # bookmarks current dir
  pwd >> $HOME/.dirsbookmarks
}