Four Tips to Moving Around the Filesystem Quickly With the Bash Shell

I used to teach Linux/Unix fundamentals and Linux sysadmin courses, which were both by nature centered around use of the shell. What I observed was that many of the frustrations of novice shell users revolved around mistakes they made while typing, and time spent trying to remember directory paths or filenames. Part of getting comfortable with the command line environment was learning how to save time and either avoid or work around these issues. Luckily, there are several tools we can use to make our life in-shell easier.

Tab Completion

The first trick is in my opinion the most useful, even in its most basic form. That is, at any point while entering a command or command argument, you can just hit the TAB key to complete it. If there are multiple choices, hitting TAB twice will show you a list. It's that simple to use. Here is how this works under the hood, from the completion section of the bash manual page:

complete (TAB) Attempt to perform completion on the text before point. Bash attempts completion treating the text as a variable (if the text begins with $), username (if the text begins with ~), hostname (if the text begins with @), or command (including aliases and functions) in turn. If none of these produces a match, filename completion is attempted.

Completion can also be programmed, for example you can specify that the shopt bash builtin command will complete from the list of available shell options. Debian-based GNU/Linux systems have a very useful bash-completion package with a lot of the most useful programmable completions enabled, including the aforementioned shopt option completion.

Change Directories With Variable Names by Setting cdable_vars

There are some directories you will find yourself cd'ing into all the time. These are good candidates to save for future use, by putting each directory name in a variable, at the same time setting the shell option cdable_vars. Here is an example of what would go in your ./bashrc, just change the directory path to suit.

shopt -s cdable_vars export doc="/home/dmaxwell/Documents"

Here is an example of how to use cdable_vars.

dmaxwell@kaylee:~$ source .bashrc dmaxwell@kaylee:~$ shopt cdable_vars cdable_vars on dmaxwell@kaylee:~$ echo $doc /home/dmaxwell/Documents dmaxwell@kaylee:~$ cd doc /home/dmaxwell/Documents dmaxwell@kaylee:~/Documents$

One note - tilde expansion, which normally allows you to use ~ as a shortcut for your home directory, will not work with cdable_vars - so if you had this in your .bashrc for example:

export doc="~/Documents" # Warning - will not work

Then typing cd doc would result in a bash: cd: doc: No such file or directory error.

Correct Spelling Mistakes On-the-fly with cdspell

It happens to all of us - transposed letters or minor case errors mean recalling the last command from history and editing it to fix the mistakes. Setting the cdspell bash shell option will fix many such minor errors for you on-the-fly. Just enable it in your .bashrc as follows:

shopt -s cdspell

Now let's see how it works, with a case error and some transposed letters:

dmaxwell@kaylee:~$ ls -ld Documents drwx------ 37 dmaxwell dmaxwell 4096 Mar 15 08:54 Documents dmaxwell@kaylee:~$ cd documents bash: cd: documents: No such file or directory dmaxwell@kaylee:~$ source .bashrc dmaxwell@kaylee:~$ shopt cdspell cdspell on dmaxwell@kaylee:~$ cd documents Documents dmaxwell@kaylee:~/Documents$ cd dmaxwell@kaylee:~$ dmaxwell@kaylee:~$ cd tpm tmp dmaxwell@kaylee:~/tmp$

The above also illustrates another nice shell shortcut - a simple cd alone on a line will always bring you back to your home directory.

Store Commonly Used Directories with CDPATH

It's not uncommon to forget where you are in a directory tree, or to forget where that latest projects subdirectory is. CDPATH can help in these situations. Start by putting something like the following in your .bashrc:

export CDPATH=":~/Documents/projects"

CDPATH is an environment variable that should contain a colon-separated list of directories. In this case, we have two - the current directory (given by the first empty string), and the directory ~/Documents/projects. When you use the cd command, if the target path is not found, the shell looks under those directories you specify in CDPATH. Let's see how it works.

dmaxwell@kaylee:~$ ls -ld Documents/projects/foo drwxr-xr-x 2 dmaxwell dmaxwell 4096 Mar 15 09:06 Documents/projects/foo dmaxwell@kaylee:~$ cd foo bash: cd: foo: No such file or directory dmaxwell@kaylee:~$ source .bashrc dmaxwell@kaylee:~$ echo $CDPATH :/home/dmaxwell/Documents/projects dmaxwell@kaylee:~$ cd foo /home/dmaxwell/Documents/projects/foo dmaxwell@kaylee:~/Documents/projects/foo$

We can see that ~/Documents/projects/foo would not normally be accessible directly from the home directory, being two levels deeper. Once we add ~/Documents/projects to our CDPATH, the shell can now find the directory foo.

More Reading