Bash Change Directory (cd) Methods, Tips and Tricks

Changing directories in a terminal may have become a thing of the past for mainstream users. However, if you do any level of system administration work, testing work, Big Data Manipulation or similar, you will soon find yourself using the Change Directory (cd) command at the Bash or Linux terminal prompt more and more.

In this tutorial you will learn:

  • Simple terminal Change Directory (cd) command usage
  • Additional methods, tips and tricks for making a richer cd experience
  • Examples highlighting the use of the various cd commands
Bash Change Directory (cd) Methods, Tips and Tricks

Bash Change Directory (cd) Methods, Tips and Tricks

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux Distribution-independent
Software Bash command line, Linux based system
Other Any utility which is not included in the Bash shell by default can be installed using sudo apt-get install utility-name (or yum install for RedHat based systems)
Conventions # – requires linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires linux-commands to be executed as a regular non-privileged user
  1. A simple change directory

    First we create two directories, named a and b using the mkdir command:

    $ mkdir a
    $ mkdir b
    

    Next we do a few simple Change Directory (cd) commands:

    $ cd a
    $ pwd | sed 's|.*/||'
    a
    $ cd ../b
    $ pwd | sed 's|.*/||'
    b
    $ 
    

    We first changed into the a directory, and then used pwd (path working directory, the current directory we are in) to see the subdirectory we are in. We also used a simple sed regular expression to filter out the leading part of the path name. For more information on regular expressions and set, please see our [Bash Regexps for Beginners with Examples](bash-regexps-for-beginners-with-examples) and [Advanced Bash Regex with Examples](advanced-bash-regex-with-examples) guides.

  2. Changing back to the previous directory

    Using the same setup as above, starting in the directory which contains the a and b subfolders, we can do the following:

    $ cd a
    $ pwd | sed 's|.*/||'
    a
    $ cd ../b
    $ pwd | sed 's|.*/||'
    b
    $ cd -
    /home/roel/a
    $ pwd | sed 's|.*/||'
    a
    

    Here we changed into the a directory like above, then used a relative path name (a relative path name is a path name which is relevant to the current position, i.e. not a full pathname like for example /home/roel/a. Next we used our special change directory command, cd - which is a handy shorthand for changing back to the previous directory we were in, in this case /home/roel/a.

  3. Changing to the home directory

    There are two methods to change back to our personal home directory. The first one is to simply issue cd without specifying any path. Let’s see how this works:

    $ cd a
    $ pwd
    /home/roel/a
    $ cd 
    $ pwd
    /home/roel
    

    Here we changed into the directory ./a by again using the cd a command. After this we checked the directory we were in, and confirmed we were in the ./a subdirectory within our home directory. Finally we issues the cd command without any options, and checked again our Path Working Directory (pwd), which correctly shows that we are back in the home directory /home/roel.

    The second method is similar. We can use ~ to refer to our home directory:

    $ cd ~/a
    $ pwd
    /home/roel/a
    $ cd ~
    $ pwd
    /home/roel
    

    Here we changed to the directory /home/roel/a by using the cd ~/a command which employs the my-home-directory shortcut ~. We subsequently changed directories again to /home/roel by using the ~ alone. This shows two methods to quickly browse either to the homedir or a directory under… or above… it. Let’s examine the __above__ bit a bit further:

    $ cd ~
    $ pwd
    /home/roel
    $ cd ~/../roel/a
    $ pwd
    /home/roel/a
    

    First we changed to the home directory (as confirmed by pwd). Then we changed to a relative directory whilst using two special methods/shortcuts, namely ~ and ... One could read this as: change directory to the home directory (~), then go up one directory (..) and next go into the directory roel you will find there, then into the directory a and hence we end up in /home/roel/a as instructed.

  4. Using .. and .

    Bash is very flexible when it comes to specifying path names:

    $ pwd
    /home/roel
    $ cd ~/../../home/../home/./roel///a
    $ pwd
    /home/roel/a
    

    Here we start in the home directory /home/roel and next we issue was looks like a command which would never parse. But, the Bash shell handles it all. The long instruction provided here is similar to doing all these separately:

    $ pwd
    /home/roel
    $ cd ~; cd ..; cd ..; cd home; cd ..; cd home; cd .; cd roel/aa
    $ pwd
    /home/roel/a
    

    Most of these will now be self-explanatory. However there are some interesting new idioms (forms of using the Bash language) in there. One of such idioms is the use of .. This in Bash means __current directory__ and basically does nothing. It is used in other things, for example when we start a binary in Bash we will usually do ./some_binary i.e. use the current directory as a prefix.

    The second one is that in the original example we used roel///a, which for Bash is identical to roel/a. Specifying multiple slashes is just translated to a single slash!

    Both these items, namely the current directory dot (.) and multiple slashes (two: // or more) are very handy when it comes to scripting, and especially the latter one. It avoids the need to have some variables cleaned up. For example when joining a fixed path from the root (the root directory is the topmost directory in your directory tree and is indicated by the first slash /) with a relative path, not sanitizing variables may lead to something like /home/roel//a which will still work perfectly fine.

Conclusion

In this article, we explored various methods, tips and shortcuts which make directory browsing at the command line a lot more enjoyable experience. Leave us a comment below with your best change directory tricks! Enjoy!



Comments and Discussions
Linux Forum