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.

For example, I do the following cds in succession-

cd /tmp
cd /home/admin
cd /root/
cd /some_other/directory

Now I am in /some_other/directory. Now, is there anyway to go back to /tmp, the directory from where I started, from the command line? Just like we navigate directories using the back button in the GUI?

cd - just takes me one level back.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

This is shell specific. In a pure POSIX shell, you can't without typing cd /tmp again. In csh, tcsh, bash or zsh, you can use pushd instead of cd to change directories and then popd +1 to cd to the first directory you pushed onto the stack.

share|improve this answer
add comment

Read this article from LinuxGazette on adding a history to cd with a few simple scripts. It's quite usable.

If you want to be fancy, then there is a program called autojump (homepage here) which keeps history of the directories visited, provides shortcuts and generally supports "intelligent" cd.

autojump is a faster way to navigate your filesystem. It works by maintaining a database of the directories you use the most from the command line. The jumpstat command shows you the current contents of the database. You need to work a little bit before the database becomes usable. Once your database is reasonably complete, you can “jump” to a commonly "cd"ed directory by typing: j dirspec

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.