Change Directory in Mac Terminal

The Terminal on your Mac allows you to navigate your computer’s file system using commands. One of the fundamental tasks you’ll need to perform is changing directories, or moving from one folder to another.

In this tutorial, we will explain how to use the cd (change directory) command to move between folders, how to navigate different parts of your file system, and some tips for efficient navigation.

Basic Command for Changing Directory

To change directories in the Terminal, you use the cd command. The basic syntax for this command is simple:

cd directory_path

The directory_path can either be an absolute path (starting from the root of the file system) or a relative path (starting from your current working directory). Let’s look at some examples to illustrate how this works.

Navigating to a Specific Directory

If you want to move to a specific directory, you need to provide the path of that directory. For instance, if you want to navigate to the Documents folder in your home directory, you would use the following command:

cd /Users/yourusername/Documents
Change Directory in Mac Terminal - Navigating to a Specific Directory

In this example, /Users/tuotrialkart/Documents is the absolute path to the Documents directory. Make sure to replace yourusername with your actual username. This command moves you directly to the Documents folder, no matter where you are in the file system, since we are using the absolute path.

Using Relative Paths

If you’re already in a directory and want to move to a subdirectory, you can use a relative path instead of the absolute path. For example, if you’re in your home directory and want to move to the Documents folder, you can simply type:

cd Documents
Change Directory in Mac Terminal - Using Relative Paths

Relative paths save time and keystrokes when navigating within closely related directories. The command cd Documents assumes you are already in a directory that contains a Documents folder.

Navigating Back to the Home Directory

If you ever want to quickly return to your home directory, you can simply use the cd command without specifying a path:

cd
Change Directory in Mac Terminal - Navigating Back to the Home Directory

This command will take you back to your home directory, no matter where you are currently located in the file system.

Alternatively, you can use the tilde (~) symbol to represent your home directory:

cd ~
Change Directory in Mac Terminal - Navigating Back to the Home Directory

The tilde symbol is a shortcut that points to your home directory, and it’s especially useful when writing scripts or navigating quickly.

Moving Up a Directory Level

If you want to move up one directory level (to the parent directory of your current directory), you can use two periods (..) as the path:

cd ..
Change Directory in Mac Terminal - Moving Up a Directory Level

This command will take you up one level in the directory hierarchy. For example, if you’re currently in the Documents folder and run cd .., you’ll be moved back to the Users/yourusername directory.

Moving to the Root Directory

The root directory is the top-most directory in your file system, represented by a single forward slash (/). To move directly to the root directory from anywhere in your system, use:

cd /
Change Directory in Mac Terminal - Moving to the Root Directory

This command places you at the root of your file system, where you can then navigate to any other part of the system using absolute paths.

Navigating to Previously Visited Directories

If you need to quickly switch back to the previous directory you were in, you can use a dash (-) with the cd command:

cd -
Change Directory in Mac Terminal - Navigating to Previously Visited Directories

This command acts as a toggle between your current and previous directory. It’s particularly useful when you need to switch between two directories frequently.

Listing Contents of the Current Directory

While changing directories, it’s often helpful to see what files and subdirectories are available. You can list the contents of the current directory by using the ls command:

ls

To get more detailed information about the files and folders in the directory (including hidden files), you can use:

ls -la

This provides additional details like file permissions, sizes, and modification dates.

Tips for Efficient Navigation

Here are a few tips to make navigating directories in Terminal faster and easier:

  1. Tab Completion: When typing directory names, you can press the Tab key to auto-complete the directory name. This is especially useful for long or complex names.
  2. Aliases: You can create shortcuts for commonly used directories by defining aliases in your ~/.bash_profile or ~/.zshrc. For example, you could set alias docs='cd ~/Documents' to quickly jump to your Documents folder by typing docs.
  3. Bookmarking Directories: You can bookmark directories with symbolic links or custom scripts, allowing you to quickly navigate to key locations in your file system.