DEV Community

Jimmy McBride
Jimmy McBride

Posted on • Updated on

Bash: How To Teleport In The Terminal


Today, I'm going to be sharing a little trick I use to teleport around my terminal with ease.

Do you change to your project directory like this?

> ~
$ cd Documents

> ~/Documents
$ cd projects

> ~/Documents/projects
$ cd my-project

> ~/Documents/projects/my-project
$ ls

src/ package.json yarn.lock etc... 
Enter fullscreen mode Exit fullscreen mode

Then this article is for you!

Pushd and Aliases

If you have directories that you commonly go to, it can be incredibly useful to create aliases using pushd.

If you don't know anything about your bashrc, pushd, or setting up aliases, check out my Bash Script Tool Kit blog.

We can try adding something like this to our bashrc:

alias projects="pushd ~/Documents/projects"
alias experiments="pushd ~/Documents/experiments"
Enter fullscreen mode Exit fullscreen mode

Now, anytime we type projects or experiments into our terminal, we can instantly "teleport" to these directories!

great Scott

But it doesn't stop there, folks! You're probably asking yourself right now, "Jimmy, why use pushd when cd would do the same thing?"

That's a very good question! Pushd has a special feature. After we pushd into a directory we can use popd to pop back to the directory we were previously in. Say we are in our experiments folder and we type in projects into are terminal. Now, if we want to go back to our experiments directory, all we have to do is type popd and we're back were we were!


&& is a very useful and powerful tool when it comes to writing bash alias. It lets us chain commands after one another in one line! So lets use this && operator to chain some extra commands and improve our scripts.

alias projects="pushd ~/Documents/projects && clear && ls"
alias experiments="pushd ~/Documents/experiments && clear && ls"
Enter fullscreen mode Exit fullscreen mode

You can see here that we've chained clear and ls to our commands. So now when we use the projects alias it will take us to our projects directory, clear our screen, and list out all the files and directories. Pretty sweet, huh?

Another alias I use quite a lot is:

alias x="clear && ls"
Enter fullscreen mode Exit fullscreen mode

CD Aliases

How many times do you do this:

$ cd ..

$ ls
Enter fullscreen mode Exit fullscreen mode

Why not set up a couple of aliases that do the hard work for you?

alias ..="cd .. && clear && ls"
alias ...="cd ../.. && clear && ls"
alias ....="cd ../../.. && clear && ls"
alias .....="cd ../../../.. && clear && ls"
Enter fullscreen mode Exit fullscreen mode

Now .. will take you up a directory, clear the screen and list out all your files in that directory for you. Pretty neat, huh?


Combine the use of chaining with && and pushd we can create some incredibly useful alias that help us not only teleport around our filesystem, but keep it clean and organized as well.

What are some of your favorite bash scripts or aliases? I'd love to hear about them in the comments!

Top comments (41)

kenbellows profile image
Ken Bellows • Edited

pushd and popd are super handy for jumping to several locations, then trying to backtrack to where you were a few jumps ago; I've known about them for a while and still haven't gotten into the habit of using them.

But for simple one-off cases, just jumping to one directory and the jumping back to the last one, there's another option that I use more often: if you call the cd command with the - character, it jumps back one directory!

> ~/projects/my-thing/src
$ cd ../../my-other-thing/sec

> ~/projects/my-other-thing/src
$ cd -

> ~/projects/my-thing/src

This only works for a single level; whereas pushd/popd will store as many moves as you want, if you issue cd - a bunch of times, you'll just jump back and forth between the same two directories. But for most of my own day to day uses, it gets the job done. This is especially handy when you weren't planning to jump back, like when you misremember where something is and you cd to the wrong place, then want to jump back to where you were.

Another related protip: the git checkout command adopted the same convention, so that git checkout - will checkout out the previous branch!

fwolfst profile image
Felix Wolfsteller

Maybe worthwile to note that zsh gives you multiple cd - entries and you can even iterate and inspect the list when hitting tab. Quite nice. And then wd plugin for "warp points" (basically cd X aliases, but with convention).

cmbell715 profile image

~- is the value of the shell variable OLDPWD, so you can list out your previous directory with ls ~- . Saves a little time from having to type all the extra characters in cd - && ls && cd - .

But since it holds the actual directory path, it's even more useful because you can quickly list out any subdirs with ls ~-/oldPwdSubDir/anotherSubdir

dom111 profile image
Dom Hastings • Edited

Another useful feature of cd, calling it with no arguments takes you to ~. This can save you doing cd ../../../.. or cd ~!

kenbellows profile image
Ken Bellows • Edited

A side effect of this that sometimes catches me off guard is that if you try to cd to an environment variable that doesn't exist, or if you misspell a variable (e.g. cd $PORJ_HOME instead of $PROJ_HOME), you end up running plain cd by accident and going home instead! Always takes me a second to realize what happened

waylonwalker profile image
Waylon Walker

I ❀️ aliases

You gotta check out rupa/z. It's not quite as reliable as an alias, it is magical. It's based on your frecently used directories.

GitHub logo rupa / z

z - jump around

Z(1)                             User Commands                            Z(1)
       z - jump around
       z [-chlrtx] [regex1 regex2 ... regexn]
       bash, zsh

       Tracks your most used directories, based on 'frecency'.

       After  a  short  learning  phase, z will take you to the most 'frecent'
       directory that matches ALL of the regexes given on the command line, in

       For example, z foo bar would match /foo/bar but not /bar/foo.

       -c     restrict matches to subdirectories of the current directory

       -e     echo the best match, don't cd

       -h     show a brief help message

       -l     list only

       -r     match by rank only

       -t     match by recent access only

       -x     remove the current directory from the datafile

       z foo         cd to most frecent dir matching foo

       z foo bar     cd to most frecent dir matching foo, then bar

       z -r foo      cd to highest ranked dir matching foo

       z -t foo      cd to most recently
i5513 profile image

I was hopping to see this kind of tools at this post

ecognito profile image
E Cognito • Edited

I use a combination of z and aliases. E.g.:

alias projects="z ~/Documents/projects"

So now, if I just type projects I'll always go to the ~/Documents/projects folder, but if I type projects img I'll have a good chance of going to the ~/Documents/projects/static/img folder.

The alias is using z, but only to change to a folder under ~/Documents/projects.

tchoupinax profile image

Otherwise, you could use a better z :)

samuelabreu profile image
Samuel Abreu

One extra of using &&, if the first command fails it stops, if you need to run all commands even when any of them returns an error you can use ;

As && you also can use ||, to execute something if the previous command returns an error, very handy on scripts, something like:

command > /dev/null 2>&1 || exit 1

To suppress stdou and stderr of command but stop the script if returns an error

jimmymcbride profile image
Jimmy McBride

That's actually really useful to know! Thanks for sharing!

bloodgain profile image

I've never gotten into the habit of using aliases for cd-ing into directories, but I use the pushd set of commands so much, I have them shortened to pu, po, and ds (for dirs -v). I don't personally have a huge use for location aliases, as I just use soft links to create shortcuts to them from my home directory. Typing ~/ isn't a huge deal to me.

That said, the "proper" or POSIX-y way to do this is to use the CDPATH variable. Much like you can set your default binary paths with the PATH variable, you can give cd a list of places to look for directories you've asked for. You should usually make . the first directory in CDPATH, but you can add as many additional directories as you like. So in your examples, if you add ~/Documents to your CDPATH, you could get to your projects folder by just typing cd projects from wherever you were. As long as the current directory didn't contain a "projects" folder, you'd be taken to your folder at ~/Documents/projects. The pushd command should obey the CDPATH as well, but I haven't tested it in a while.

vavrikvladimir profile image

Hello Jimmy,
thanks for article.
Please check the code in the CD Aliases section. There are missing the &&.
The correct code should be:

alias ..="cd .. && clear && ls"
alias ...="cd ../.. && clear && ls"
alias ....="cd ../../.. && clear && ls"
alias ....="cd ../../../.. && clear && ls"
jimmymcbride profile image
Jimmy McBride

Thank you very much! Good catch. The changes are saved now. :)

selim13 profile image
Dmitry Seleznyov

For entering frequently visited directories I really enjoy combination of

  • z frequently visited directories tracker
  • fzf command line fuzzy finder
  • fzf-z plugin that combines the above

Then CTRL-G dev will show me the list of all most used directories with dev in their name sorted by the usage frequency. Hitting enter with enter the directory.

andyanderson profile image
Andy Anderson

Great idea, Jimmy! I’ve used aliases with pushd/popd for decades, actually, but never thought to include an automatic ls on the end. For example:

alias ,=pushd

This uses one other nice feature of pushd β€” by itself it swaps the top two items on the directory stack! So you can quickly go back and forth between two directories with a single comma.

Including && ls at the end of this, though, prevents applying it in general, as any trailing file/directory arguments are picked up by the ls. But if you instead use a shell function:

function , { pushd $1 && clear && ls; }

it works both with and without an argument.

Also, if you don’t mind occasionally typing out source, you can repurpose . :

alias .=popd

I’m using . here because it’s next to ,, it signals finality, and there really aren’t any other punctuation keys available without shift that aren’t interpreted by the shell for something else πŸ˜€

I really like the .. idea, though I doubt I would use more than one level very often. One other suggestion I would make is to perhaps provide a variant,

alias ,,="pushd .. && clear && ls"

to add the parent directory to the directory stack.

Finally, sometimes I want to remind myself of the current directory stack, which is displayed with dirs. Again, being short on punctuation keys but given this is not that common, I can alias it to ,., which is in keeping with the rest of these options:

alias ,.=dirs
mehuge profile image

Supercharge your cd command with something like this:

function goto() { eval "$(~/bin/goto "$@")"; clear; ls; }

Then in ~/bin/goto you can put extended logic that will find where you want to go, for example

GO() {
  if test -d "$1" ; then
    echo "builtin cd $1";
    exit 0;

case "$1" in
somehost) echo "ssh" ; exit 0 ;;
work) echo "builtin cd /path/to/work/Projects"; exit 0 ;;

GO "$HOME/$1"
GO "/d/dev/$1"
GO "/c/GAMES/$1"

exit 0

Finally, if you prefer to use cd rather than goto add an extra alias:

alias cd=goto

Example Usage:

cd somehost
cd myproject
alexmartelli profile image
Alex Martelli

A small typo in:
alias ..="cd .. && clear && ls"
alias ...="cd ../.. && clear && ls"
alias ....="cd ../../.. && clear && ls"
alias ....="cd ../../../.. && clear && ls"
the 4th alias is identical to the 3rd one, so will over-write it; I imagine you meant the 4th alias to be five dots, not just four like the 3rd one.

jimmymcbride profile image
Jimmy McBride

Fixed! Thank you!

rrei profile image
Rui Rei

Good post :)

While I recognize the value in setting up custom aliases, IMHO, fzf ( is all I need. I can type like a raging lunatic monkey and most of the time it still guesses what I actually wanted to type. fzf changed my life!

teraspora profile image
John Lynch

Make aliases more easily:

# Quickly make a persistent alias
echo "alias $1='$2'" >> ~/.bash_aliases
. ~/.bash_aliases
jimmymcbride profile image
Jimmy McBride

This is leet af! I'm stealing this for sure.

gabrielizalo profile image
Gabriel Porras

These are my aliases and adding yours... Very cool recommendations. Thanks

alias la="ls -lart"  or  la="ls -lrt"
alias e="exit"
Enter fullscreen mode Exit fullscreen mode
bew profile image
Benoit de Chezelles

Hey, instead of typing e or exit, you can do Ctrl-d, it will do the same as exit in almost all cli apps!

Basically Ctrl-d sends a special control character to the application, saying "there is nothing left to read!" which will usually close the cli app.

antoniogamiz profile image

Greaaaaaaat!!!! pushd is just amazing! I will miss typing cd on my terminal from now on. Thanks!

piyushkmr profile image
Piyush Kumar Baliyan

I love aliases, laziness converted to productivity hacks.
Here is my implementation of common git shortcuts, along with some common patterns using bash functions.

jimmymcbride profile image
Jimmy McBride

I didn't know you could do git aliases like that. Really cool stuff. Thanks for sharing! πŸ”₯

mallowigi profile image
Elior Boukhobza

I've been using autojump for mac/unix for a while now. Just using j <part of the dir> and it "teleports" you automatically to your last recent places. The only thing needed is to cd at least once to the repository.