Shell autocompletion of directory and file names makes navigating through your filesystem quite a bit quicker than if you had to type all of the paths in full, but there are a few other ways to make navigating through and between long pathnames on your filesystem a little bit easier.
As a first note, typing
cd with no arguments will take you straight back to
HOME directory; you don’t actually need to type
cd ~ or
cd $HOME in
You can move to any particular user’s
HOME directory with the general form
To navigate to the directory you were in before the last
cd call, you can use
cd -. This will both move you back into that directory and echo the pathname
it was on for you.
$ cd projects
$ cd -
If you are working mostly within one directory and need to change to another
briefly for some reason, rather than using
cd you can use
pushd to move to
that directory and add it onto the directory stack. You can move to further
pushd successively, and then when you’re done in each one,
popd will take you a step backward again. Each time you call
popd, it will print the current contents of the directory stack:
$ pushd projects
$ pushd /usr/local/bin
/usr/local/bin ~/projects ~
By default, you can move around relatively in the filesystem tree by typing
dir for any subdirectory of your working directory, rather than its full path.
You can generalise this to provide a list of directories to check successively
for matching subdirectories:
$ export CDPATH=.:~
In this case, if I were to type
cd projects, if there were no subdirectory of
the working directory by the name of
project but there was one called
/home/tom/project, I would be sent there instead. This probably isn’t a good
thing to set in any non-interactive terminal, but it can be a nice shortcut for
interactive shells. Note that the first part of the list should always be
for the working directory.
This has the side effect of printing the complete path to which you just moved on a line before your prompt if it’s used. I happen to find this quite helpful.
In an interactive shell, if you make a small mistake in typing a path for
that means it doesn’t resolve to an actual directory, it’s generally safe to
have the shell correct mistakes for you and move you into the directory you
most likely intended:
shopt -s cdspell
This will correct things like dropped or swapped characters in the path you typed:
$ cd /home/tmo
If you know you’re going to be switching back and forth between several directories, it often makes sense to put them into variables for quick changing, or even just editing files directly without having to move around at all:
$ vim "$acnd"/httpd.conf
$ vim "$sanc"/index.html
$ cd "$abin"
$ ./apachectl configtest
$ ./apachectl restart
This last one may seem like a very elementary trick, or something that you’d
typically only do within a shell script. However, if you use consistent
variable names or even put them into a startup file like
actually quite surprising how much time it can save you; you start to realise
how much time you were spending typing out paths.