Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
2  
Can you explain some more? is cd - what you want? –  Philomath Jul 29 '11 at 8:12
add comment

4 Answers

up vote 8 down vote accepted

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!

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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