cd (Change Directory)

cd stands for change directory. After you type cd, you give this command (and nearly all Unix commands) an argument. You do this by typing cd , and then a space, and then the argument at the terminal prompt

When you change the directory, you are changing the current working directory of the shell prompt itself to the new directory you specify. That means you “move” to the new location specified.

The first argument to cd is a relative folder location. (Remember, you can only specify folders or directories here— not files.)

Remember, as we covered in the last lesson, you begin your relative path in one of four ways:

  1. Using .., which we learned about in the previous lesson, tells the prompt to go up one directory or to the parent directory. You can move up two directories using ../.., or three directories using ../../.., etc.
  2. Using the name of a directory within the current directory, we can go down one directory into the directory specified.
  3. Using the home directory specifier (~), we can go to the home directory or another directory relative to the home directory. (We learned in the last lesson that ~ means the home directory.)
  4. Using the forward-slash (/) at the start of the filepath indicates the hard drive root itself. You can then also use this to specify a full path (meaning, the path relative to the root), like /abc/xyz.

When you cd, you are changing the directory of your current working directory for the shell prompt you are currently using. Your next command will be interpreted from your new working directory. The new one might be relative to the CWD (as in the case with a relative path like . or ..) or it might be non-relative (as in the case of ~ and /). Either way, your current working directory will be changed. That’s this command is called change directory or cd for short.

Do not confuse this forward slash that comes as the very first character with other forward slashes that might come, for example, after a dot (.), two dots (..), or tilde (~). Those file forward slashes do not indicate the root of the hard drive.

Let’s move around our hard drive now using the following seven exercises.

Exercise 1: Move To Your Home Directory

cd ~

Type cd followed by ~ and hit return.

Exercise 2: Move the Root of Your Hard Drive

cd /

Type cd followed by a single forward slash (/) and hit return. (*Do not confuse the forward slash / with the backslash \)

Exercise 3: Move Back To Your Home Directory

cd ~

Same as exercise 1. You are now back in your home directory.

Exercise 4: Move Up One Directory

cd ..

Type cd followed by a space and then two dots (periods). Period characters are called “dots” in the context of programming. Hit return.

Notice that in macOS, you are now in the /Users directory. For WSL (Ubuntu) and most Unix systems, your user directories will be in /home instead.

Exercise 5: Move Down One Directory (back into your home directory)

cd yourname

(Instead of yourname you will type the name of your home directory.)

Exercise 6: Move Down One Directory (into any folder of your choosing in your home directory)

Pick any folder that you know is in your home folder. For example, I’m going to choose Pictures (it comes default with macOS, but this folder might not be present on non-Mac systems.)

cd Pictures/

I have now changed directory into the Pictures/ directory.

Exercise 7: Move Up Two Directories

cd ../..

Notice that we are back at /Users (or /home), where we were when we ran the command in Exercise 4.

When you have a solid understanding of how to move up a directory, move down a directory, to the root of the drive, and to your home folder, move on to the next lesson.

Let’s revisit something we learned in Lesson 4: Bash and Zshell

If your system is using Bash, you’ll have these files at the location of your home folder.

.bash_profile
.bash_login
.bashrc

If you have ZShell, you’ll instead have these files:

.zprofile
.zsh_history
.zsh_sessions
.zshrc

First, change directory to your home directory with

cd ~

Now, let’s examine the home directory itself for files beginning with with either .bash or .zsh