cd: no such file or directory error in Bash and ZSH [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# cd: no such file or directory error in Bash and ZSH [Fixed]

The bash and zsh error "cd: no such file or directory" occurs for multiple reasons:

  • Misspelling the name of the directory (directory and file names are case-sensitive).
  • Mixing up relative and absolute paths (e.g. cd Desktop vs cd ~/Desktop).
  • Trying to cd to a path that doesn't exist on the file system.
  • Having syntactical errors when issuing the cd command.

To solve the error, make sure the path exists and is spelled correctly.

cd no such file or directory

# Misspelling the path you are trying to cd to

Make sure you haven't misspelled the path.

Names of files and folders are case-sensitive on macOS and Linux.

For example, the following two folders are distinct.

shell
My-Folder my-folder

names of paths are case sensitive

Notice that the cd my-folder command succeeds whereas the cd My-Folder command fails.

Names of directories and files are case-sensitive, so make sure your spelling is correct.

Similarly, make sure you haven't mistaken lowercase L for uppercase I (e.g. l vs I) or the letter O vs the number 0.

These letters are difficult to distinguish and are often a cause of the error.

# Absolute vs Relative paths

Another common cause of the error is incorrectly using a relative or an absolute path.

A relative path is specified relative to the currently active terminal directory.

You can use the pwd command to print the current working directory.

shell
pwd

print current working directory

For example, assuming we have the following folder structure.

shell
bobbyhadz-js/ # ๐Ÿ‘ˆ๏ธ you are here โ””โ”€โ”€ my-folder/

If my terminal is located in the bobbyhadz-js directory, I can use the following commands to cd into my-folder.

shell
cd my-folder cd ./my-folder

using relative paths

A relative path is specified by no prefix at all or the ./ prefix.

When you issue the cd my-folder command, the specified folder has to be located in the current directory.

You can also use the ../ prefix if you have to navigate one directory up.

Assuming we have the following folder structure.

shell
bobbyhadz-js/ โ””โ”€โ”€ my-folder/ # ๐Ÿ‘ˆ๏ธ you are here views/

If my terminal is located in the my-folder directory, I can use the following command to switch to the views directory.

shell
cd ../views

switch one directory up

You can use the ../ as many times as necessary, e.g. cd ../../some-folder to go 2 directories up and then into the specified folder.

On the other hand, an absolute path is used to specify the complete path to the directory.

Here are 2 equivalent examples of using absolute paths.

shell
cd /home/user/Desktop cd ~/Desktop

using an absolute path

The / (root directory) and ~/ (user's home directory) prefixes are used to specify an absolute (complete) path.

The tilde ~ symbol is used to refer to the user's home directory (e.g. /home/john if your username is john).

shell
echo ~

meaning of tilde bash

The error is often caused when you use a relative path incorrectly.

For example, cd Desktop vs cd ~/Desktop.

shell
# โ›”๏ธ cd: no such file or directory: Desktop cd Desktop # โœ… Correct cd ~/Desktop

relative vs absolute path

The command above causes the error because there is no Desktop directory in the current folder.

However, if you use the / (root directory) or ~/ (user's home directory), you can specify an absolute path, regardless of the current position of the terminal.

The ~/Desktop command expands to /home/john/Desktop and navigates to the user's Desktop directory.

You can list the directories in your user's home directory with the ls ~ command.

shell
ls ~

If you see the directory you are trying to change to, use the ~ prefix, e.g. cd ~/Downloads.

shell
# โ›”๏ธ Incorrect (relative) cd Downloads # โœ… Correct (absolute) cd ~/Downloads

Similarly, you can use a prefix of a / to start at the root directory.

shell
cd /home/your_user/Desktop

You can use the echo $USER command to get your username.

shell
echo $USER

get current username

You can use the ls / command to list the folders that are contained in the root directory.

shell
ls /

# Make sure the path exists

If the error persists, make sure the path you are trying to cd to exists.

You can use the ls command to print the files and folders in a given directory.

shell
# Relative path ls my-folder # Absolute path ls ~/Desktop/my-folder

list files and folders under path

Make sure the folder you are trying to cd into exists in the given folder.

If you also use the test command to check if a path exists.

shell
if test -d /path/to/dir; then echo "exists"; fi

Make sure to replace the /path/to/dir placeholder with your given path.

check if path exists

If the path exists, then a confirmation message gets printed, otherwise, nothing is printed.

If you ever get confused about the currently active terminal directory, use the pwd command.

shell
pwd

print current working directory

# Use a forward slash as the separator for path components

On macOS and Linux, a forward slash / is used to separate path components.

shell
# Correct for macOS and Linux /path/to/dir /home/john/Desktop

Here is a screenshot that demonstrates how using a backslash as a separator causes the error on macOS and Linux.

use forward slash to separate path components

The backslash \ is used as an escape character in a path and not to separate path components.

A backslash \ is used as the separator on Windows.

shell
# Windows C:\Users\john C:\Program Files\Git\bin

# Changing to your root directory or your user's home directory

If you need to change to your root directory (/), issue the following command

shell
cd /

Similarly, if you need to change to your user's home directory (e.g. /home/john), use the following command.

shell
cd ~

You can always use the pwd command to print the current working directory if you get confused.

shell
pwd

If you need to print the files and folders that are located in the current directory, use the ls command.

shell
ls

# Alternative fixes

Here are some alternative solutions if the error persists:

  1. Use the Tab key for auto-completion
  2. Open your terminal directly in the destination directory
  3. Does your path contain spaces?
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev