How to Change Directory in Terminal Mac

If you are new to Mac Terminal and just learning some of the basic commands, it’s possible you may be feeling overwhelmed. Don’t worry, once you begin using it on a regular basis, things like changing directories will become second nature to you, and typing them in will be a breeze.

Changing directories is probably one of the most frequently used commands when working in a command-line environment such as Mac Terminal. It’s a simple two-letter command followed by the directory name or path (shown below) but things can get a little complicated.

cd <file path>

I’m Eric, I have been around computers for over 40 years and I have worked as a software engineer for over 20. I have used command-line interfaces like Mac Terminal for much of that time and commands like cd are ingrained in my head so they come naturally to me.

While this command can be very simple, there are some things to know and look out for. Keep reading, and I will explain a bit further how to use it and give you some tips to make it a little easier to use.

What is the cd Command?

As I mentioned above, the cd command, which stands for change directory, is one of the most commonly used commands in UNIX/LINUX-based command-line interfaces such as Mac Terminal. This is the command that is used to navigate the file systems that you have access to.

File systems are made up of a hierarchy of directories. The top directory is referred to as the root directory, and directories underneath that are called sub-directories. This can be seen on your desktop in finder when you look at the folder structure.

In Finder, you can simply click down through the folders or subdirectories to get where you want to go but this is not possible from the command line. 

The cd command allows you to navigate down the subdirectories or back up the hierarchy to the root directory by typing in their paths.

What is a Path?

A path is just what the name indicates. It is a pathway or directions that you give to a command that tells your computer how to navigate or traverse the hierarchy of subdirectories to get to where you want to go in the file system.

A path must follow the hierarchy and paths that start from the root directory are called absolute paths. These can be used from anywhere on the file system. A path that is given from your existing location in the file system is called a relative path since it is relative to where you are.

Example Directory Structure

From our example above, an absolute path might be

/Applications/newdevelopment/App1/Logs

An example of a relative path would be if we are already sitting in the /Applications/newdevelopment directory.

App1/Logs       (notice there is no / before the path specification)

This path is relative to /Applications/newdevelopment

An easy way to tell the difference between an absolute path and a relative path is that an absolute path begins with a / and a relative one does not. This is because the absolute path always starts from the root directory which is defined as /.

Mac Terminal uses a UNIX/LINUX-based style of commands so paths are formed by putting the names of directories together with a / in between each directory name, with the root directory always being denoted as just the / by itself. 

You would move to the root directory with the following command.

cd /

If you wanted to move to the newdevelopment directory from anywhere on the file system, you could type:

cd /Applications/newdevelopment

Once in the newdevelopment directory, if you wanted to move to the App1 directory, you would only need to type

cd App1   (this uses the relative path)

But typing the absolute path will also work.

cd /Applications/newdevelopment/App1 (the absolute path) 

If I wanted to move to the decomm directory from this location I would either need to use an absolute path or I could specify upper level directories using the .. which stands for the parent directory. I will need to move up two levels in order to get to the decomm directory.

This means I would need to use two sets of .. separated by / which would result in the command shown below.

cd ../../oldprojects/decomm

Remember that relative is relative to where you currently are in the structure. You can reach any location using a relative path as long as it is formed correctly by moving up or down the directory hierarchy, but in some cases, it’s easier to use the absolute path.

Common Difficulties

Typing Mistakes

Nearly all errors that occur when using the cd command come from an improperly formed path. Many are just typing mistakes or leaving out a / somewhere, especially at the beginning of an absolute path, but most come from improperly formed relative paths.

Directory Does not Exist

Specifying a directory that does not exist will also cause an error. Whether it does not exist at all or whether it does not exist in the location you have specified. If the path cannot be followed to the location it will not work.

Including a File in the Path

Another problem that some run into is adding the file name into the path. When trying to change directories using the cd command, the path cannot contain a filename because you cannot cd to a file, only to a directory location.

Spaces in the Path

One more common problem is accidentally (or purposely) having a space in your path. The command does not like spaces and this will cause it to fail. Some directory names do have spaces in them so you will need to use quotation marks to properly execute the command.

The cd command has only one parameter, the path. The key to ensuring your cd command works properly is ensuring your path is formed properly. Below are a few tips and tricks that can help you to move around your file system a little easier.

Some Quick Tips and Tricks

The cd command is quite simple but it is easy to make mistakes and can often require a lot of typing for long paths. Some of the tips and tricks below may help you to use it more efficiently. 

  • Type cd / to move to the root directory.
  • Type cd ~ to move to your home directory. 
  • Type cd .. to move up one directory level.
  • Use the .. to create relative paths. If I am in the App3 directory as shown above I can use cd ../App2 to move to the App2 directory. 
  • A single dot (.) Represents your current directory. 
  • Use cd – to move to whatever directory you were previously in.
  • Copy and paste whenever possible. This reduces the amount of typing which in return reduces typing errors. 
  • If a directory name in your path has a space in it, enclose the path in single quotation marks so that the space does not cause the command to fail.

Final Words

The cd command is one of the most used commands when using Mac Terminal. It is what seems to be a simple command with only one parameter which is the path, but it can often turn complicated when trying to figure out the correct path and how you need to get to your location.

Paths must follow the proper hierarchy of the directory structure in the file system or your command will not work. It can be frustrating to learn how to move around the filesystem as a new user, but the more you do it the better you will get.

I hope the information I have provided above has helped you to learn how to use this command. As always, please let me know if you have any questions or problems.

Leave a Reply

Your email address will not be published.