Linux for Windows Developers - Linux basics - change directory and make directory

Changing directories - cd

Changing directories is an essential operation no matter what your interface is. In a graphical user interface such as Explorer, you’ll navigate and change directories by using the tree menu structrue. At the command line, navigating and changing directories uses a command called cd which stands for “change directory”. Just like listing a directory that you learned earlier, the cd command takes an argument which is the directory we want to change to.

Linux doesn’t use the concept of drive names like Windows does, rather a file system has a root directory and any drives are mounted under specific directories in the file system. The root of your filesystem is represented by a single forward slash (/) in Linux. To change directories to the root directory, you simply append the forward slash to the cd command. Try it - start up your WSL and type in:

$ cd /

You can verify that you are in the root directory by using pwd that you learned last time.

$ pwd

Do a quick directory listing while you are in the root directory and glance over the typical directories in a Linux install:

$ ls
acct  boot   data  etc   init  lib64       media  opt   root  sbin  sys  usr
bin   cache  dev   home  lib   lost+found  mnt    proc  run   svr   tmp  var

Remember Linux uses the forward slash (/) to separate directory names, not the backslash (\) like Windows. If you mistakenly type cd \, don’t panic, instead just hit ctrl-c and you’ll be returned to a command prompt so you can try again.

Change to a specific directory with a fuly qualified path

Any path in the filesystem can be represented as a fully qualified path. A fully qualified path is always represented as starting wth a forward slash so we are basically specifying a directory from the root of the filesystem. You can visualise this as a tree if you want. The root, trunk, branches and leaves all being directories in their own right.

To change to a directory using it’s full path, you just append the path to the cd command. To get to the /etc/init directory, for example, type the following:

$ cd /etc/init
$ pwd

Changing directories using relative paths

Relative paths are paths in relation to your current working directory. When working with relative paths, a single dot (.) means current directory and two dots (..) mean one directory up in the tree. Unlike navigating to a specific directory where you began the path with a forward slash, when navigating by relative path, you omit the initial forward slash.

Using a combination of .. and path names, you can quickly move to a directory adjacent to you current directory. Follow along with this example (remember, only type the lines preceded with a $, the other lines are echoed back to you by the commands you’ve typed in):

$ cd /etc
$ pwd
$ cd ../opt
$ pwd

This was a fairly small traversal between two directories that sit at the same hierarchical level to each other - the /etc and /opt directory. These both sit directly under the root directory. You started by changing to the /etc directory and then checked which directory you were in with pwd for some visual feedback. The system responded and showed that you were now in /etc. Next you changed directories to the /opt directory by using a relative path - you basically said “change directory by going up one level and then moving down into the opt directory”. Lastly, you called pwd again to verify that you were in fact in the /opt directory.

Note that no preceeding slash is needed when changing directories using relative paths. Try something a little more difficult - first navigate to /ect/init and then, using relative paths, change to /opt.

$ cd /etc/init
$ cd ../../opt

These are trivial examples, and really using ../ isn’t actually more efficient than simply typing the full path (so for example if you are in /etc and you wish to change into /opt, it’s quicker to simply type cd /opt than cd ../opt) but it serves to illustrate the point - that you can navigate with relative paths. It can be convenient if you have a deeply nested directory structure where using realtive paths will save you keystrokes over using a fully qualified path.

If you wish to navigate into a directory that is directly below your present working directory, you can use the directory name by itself. For example, starting in /etc, the following will move you into /etc/init:

$ cd init

You can also use the slighly longer version of actually specifying the current directory with a single dot (.):

$ cd ./init

Just like using two dots to represent one directory up, using one dot represents our current directory and anything following it should be relative to the current working path.

Your home directory shortcut

Just like Windows, Linux assigns each user a home directory. Because you’ll do a lot of work in your home directory, it has a special shortcut. The home directory is represented by a ~. To navigate to your home directory from anywhere in the filesystem, simply change directory to ~:

$ cd ~

To navigate to directories under your home directory, append the path relative to you home directory after the ~. For example, if you have a home directory and inside that directory you have a Documents folder, to navigate there quickly you would type cd ~/Documents

Make a directory

Navigating around a pre-existing file system is all well and good, but you also need to know how to make directories within the filesystem so you can organise your work. We use the mkdir command (make directory) to create directories on the file system. Let’s create some directories in our home directory:

$ cd ~
$ mkdir development
$ cd development

In this sequence of commands, you started out changing into your home directory. Then you created a directory name development and finally changed into that directory.


Here are a few exercises so you can practice creating directories and moving about your file system.

  • Create a directory called projects under the development directory you created above.
  • Create a directory called scratch under the development directory.
  • Change directories to the root (/) of the file system.
  • Change into the projects directory you created above from the root (/) directory. Hint - use ~ to represent your home directory.

Summing up

This post introduced two commands - cd and mkdir which allow you to change directories and make directories respectively. You learned how to move around the file system using cd. The root of the filesystem is represented by a single forward slash / and paths can be both absolute (for example /etc/init) and relative. Relative paths are expressed using a single dot . to specify the current directory and two dots .. to represent one directory up. You also learned a shortcut for changing to your home directory using the tilde (cd ~).