6

I'd like to be able to type in terminal commands of forward/back especially if I went from a directory with a super-long name to home (and want to return back to the directory with the super-long name).

  • 2
    Can you explain some more? is cd - what you want? – Philomath Jul 29 '11 at 8:12
8

pushd and popd can be very useful. For example try


$ pushd somedir

and when you are done, just do


$ popd

and you are back where you started.

The best part is you can do


$ pushd somedir

$ pushd anotherdir

$ pushd onemoredir

and then you can "step back" one at a time using popd.

Good luck!

18

The command cd - will take you back to the previous directory.

Also look into the pushd and popd commands which can be used to basically save an ordered list of directories you were in and go back to them.

3

As well as cd - and pushd and popd which others have mentioned, there is also cd $OLDPWD. usually cd $OL<TAB> is enough, auto-completion at work.

When using pushd and popd, bash's dirs builtin can really help out, for example: dirs -v prints the directory stack with one entry per line, prefixed with its position number in the stack. try help dirs for more.

Note that if you want change to a directory named -, should you have one, the above feature poses a problem. you will need to do cd ./-. better still, don't give your directories such names.

0

There is a script that,by typing cd --, allows one to switch to one of the ten last visited directories whilst being in a particular Bash shell. Here is an example of its use:

lotzmana@safe$ cd work
lotzmana@safe$ cd scripts
lotzmana@safe$ pwd
/home/petarma/work/scripts
lotzmana@safe$ cd --
 0  ~/work/scripts
 1  ~/work
 2  ~
lotzmana@safe$ cd -2
lotzmana@safe$ pwd
/home/petarma

The script was published by Petar Marinov in the Linux Gazette, Issue #109, December 2004 and also a month earlier on geocities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.