Even though there are some good answers already, I thought I'd mention for completeness the old pushd
and popd
Bash builtins with allow you to move very quickly between directories in deep paths in your filesystem. In contrast to the autojump
bookmarks mentioned by burger.ga, the directory stack created by using pushd
is only temporary.
As the GNU manual notes, pushd
and popd
are Bash builtins used to construct a temporary directory stack that is a
list of recently-visited directories. The pushd builtin adds directories to the stack as it changes the current directory, and the popd builtin removes specified directories from the stack and changes the current directory to the directory removed. The dirs builtin displays the contents of the directory stack.
It takes a bit of getting used to, but there are plenty of useful tutorials out there at this blog and this this site for example. There are no separate manpages for the commands, as they are included in the Bash manpage, but for quick reference you can view the GNU Bash page.
The clearest explanation is in this very useful article and is exactly the way I use pushd
and popd
:
Firstly, use dirs
or dirs -l
to list directories on the stack and dirs -c
to clear the stack.
In a sense you bookmark the location where you want to return by entering
pushd /home/mike/Pictures/Canon/2012_07_01
and then you can add more directories to the stack, so you get a dirs listing as below, which includes 3 designated folders and your ~
home folder:
dirs
~/Downloads/folder/interest ~/Music/artist/album ~ ~/Pictures/Canon/2012_07_01
You actually don't need to use popd
straightaway as that removes directories from the stack; the best thing to do is to use pushd
to rotate the stack, so that you can keep switching between the directories and order them as you want. For more on that useful stack rotation see the Bash manpage.
If you have exactly the stack arrangement as the above dirs
listing shows, pushd +3
switches you to the specific Pictures folder and places that on top of the stack (it is +3 and not +4 as you do not count your ~
in the directory stack):
pushd +3
~/Pictures/Canon/2012_07_01 ~/Downloads/folder/interest ~/Music/artist/album ~
So the prompt reads,
~/Pictures/Canon/2012_07_01$
You can keep on doing this without removing them from the stack, although any folders you cd
to, other than those added to the directory stack with pushd
, will alter your directory stack.
Once you have the directory stack arranged in the order you want, you can use popd
to quickly cycle through the directories and then return to the home folder; for example, from Pictures
we can return to Downloads
:
popd
~/Downloads/folder/interest ~/Music/artist/album ~
and the prompt reads
~/Downloads/folder/interest$
In general the beauty of this is that you can set up the directory stack and the order of items within it with pushd
and then, say you have three files to edit in each of those directories, you can return instantly to each one with popd
while removing them from the directory stack. Then with your last popd
you will return to ~
. It becomes particularly useful when you have particularly deep directories and can use it to set up the directory stack to quickly move between them.
In addition, this question contains some tips that you might find useful when working on the command-line: