cd: too many arguments error in Bash and ZSH [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
3 min

banner

# Table of Contents

  1. cd: too many arguments error in Bash and ZSH
  2. Wrap the name of the directory in double quotes
  3. Use a backslash to escape the spaces
  4. Use the Tab key for auto-completion
  5. Open your terminal in the destination directory

# cd: too many arguments error in Bash and ZSH [Solved]

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.

cd too many arguments in bash zsh

Here is an example of a command that causes the error.

shell
cd my new folder

example of command that causes error

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.

shell
# ⛔️ cd: string not in pwd: cd my folder

cd string not in pwd

The solution to both errors is the same.

# Wrap the name of the directory in double quotes

One way to solve the error is to wrap the name of the directory in double quotes.

shell
cd "my new folder"

example of command that causes error

You can also wrap an entire path to the directory in quotes.

shell
cd "my folder/my nested folder"

wrap entire path to directory in quotes

You can also wrap only the path components that contain spaces or special characters in quotes.

shell
cd my_folder/"my nested folder"

wrap only necessary path components in spaces

# Use a backslash to escape the spaces

You can also resolve the error by using a backslash to escape each space in the path.

shell
cd my\ new\ folder

using backslash to escape each space

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.

shell
cd bobbyhadz-js/my\ new\ folder

backslash escape nested path

I used a forward slash / as the separator between the path components and then used a backslash \ to escape each space.

# Use the Tab key for auto-completion

You can also solve the error by using Tab auto-completion:

  1. Start typing the path.
  2. Press the Tab key to get auto-complete directly in your terminal.

Here is a short clip that demonstrates how this works.

using tab auto completion

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.

# Open your terminal in the destination directory

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:

  1. Open the directory in Explorer.
  2. Right-click and select Open in Terminal.

open folder in terminal linux

Here is a short clip that demonstrates how this works.

open folder in terminal linux

On macOS, you can:

  1. Open a Finder window and navigate to the folder.
  2. If you don't see the path bar at the bottom of the Finder window, choose View -> Show Path Bar.
  3. Press Ctrl and click the folder in the path bar.
  4. Select Choose Open in Terminal.

You can also:

  1. Open the folder in Finder.
  2. Click on Finder in the top menu, then hover over Services.
  3. Select New Terminal at Folder.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.