Last updated: Apr 5, 2024
Reading timeยท4 min
The bash
and zsh
error "cd: no such file or directory" occurs for multiple
reasons:
cd Desktop
vs cd ~/Desktop
).cd
command.To solve the error, make sure the path exists and is spelled correctly.
cd
toMake 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.
My-Folder my-folder
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.
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.
pwd
For example, assuming we have the following folder structure.
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
.
cd my-folder cd ./my-folder
A relative path is specified by no prefix at all or the ./
prefix.
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.
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.
cd ../views
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.
cd /home/user/Desktop cd ~/Desktop
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
).
echo ~
The error is often caused when you use a relative path incorrectly.
For example, cd Desktop
vs cd ~/Desktop
.
# โ๏ธ cd: no such file or directory: Desktop cd Desktop # โ Correct cd ~/Desktop
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.
ls ~
If you see the directory you are trying to change to, use the ~
prefix, e.g.
cd ~/Downloads
.
# โ๏ธ Incorrect (relative) cd Downloads # โ Correct (absolute) cd ~/Downloads
Similarly, you can use a prefix of a /
to start at the root directory.
cd /home/your_user/Desktop
You can use the echo $USER
command to get your username.
echo $USER
You can use the ls /
command to list the folders that are contained in the
root directory.
ls /
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.
# Relative path ls my-folder # Absolute path ls ~/Desktop/my-folder
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.
if test -d /path/to/dir; then echo "exists"; fi
Make sure to replace the /path/to/dir
placeholder with your given path.
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.
pwd
On macOS and Linux, a forward slash /
is used to separate path components.
# 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.
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.
# Windows C:\Users\john C:\Program Files\Git\bin
If you need to change to your root directory (/
), issue the following command
cd /
Similarly, if you need to change to your user's home directory (e.g.
/home/john
), use the following command.
cd ~
You can always use the pwd
command to print the current working directory if
you get confused.
pwd
If you need to print the files and folders that are located in the current
directory, use the ls
command.
ls
Here are some alternative solutions if the error persists: