Last updated: Apr 5, 2024
Reading time·3 min
The "cd: too many arguments" error in bash
and zsh
occurs when we try to
cd
into a directory whose name contains spaces or special characters.
You can solve the error by wrapping the directory name in double quotes or by using a backslash to escape each space.
Here is an example of a command that causes the error.
cd my new folder
We a trying to cd
into a directory whose name contains spaces, so bash
assumes that we are passing multiple, space-separated arguments to the cd
command.
You might also get the error "cd: string not in pwd:" when you try to cd into a path that contains spaces.
# ⛔️ cd: string not in pwd: cd my folder
The solution to both errors is the same.
One way to solve the error is to wrap the name of the directory in double quotes.
cd "my new folder"
You can also wrap an entire path to the directory in quotes.
cd "my folder/my nested folder"
You can also wrap only the path components that contain spaces or special characters in quotes.
cd my_folder/"my nested folder"
You can also resolve the error by using a backslash to escape each space in the path.
cd my\ new\ folder
Backslashes are used to escape special characters, so they can be treated literally.
For example, escaping a space with a backslash means "Treat the space as a literal space character and not as a separator between arguments".
Using this approach is a bit tricky because each space has to be escaped by a backslash.
It also gets a bit difficult to read when the path is deeply nested.
Here is an example.
cd bobbyhadz-js/my\ new\ folder
I used a forward slash /
as the separator between the path components and then
used a backslash \
to escape each space.
You can also solve the error by using Tab
auto-completion:
Tab
key to get auto-complete directly in your terminal.Here is a short clip that demonstrates how this works.
If you have multiple folders with similar names, you might have to type the
first few characters of the name to identify the correct folder you want to cd
into.
If you don't get autocomplete when you start pressing Tab
, then the specified
path doesn't exist.
If you still aren't able to cd
into the directory, you can open your terminal
directly in the destination folder.
For example, on Linux, you can:
Here is a short clip that demonstrates how this works.
On macOS, you can:
Ctrl
and click the folder in the path bar.You can also:
You can learn more about the related topics by checking out the following tutorials: