Written byVain Rowe
[Solved!] Bash: cd: too many arguments/Zsh: cd: string not in pwd:
Summary: The cd: too many arguments error appears when you run cd on the command shell to enter the relative directory on your devices. This article from iBoysoft guides you to get this trouble fixed with easy but effective solutions.
You must be puzzled by the error code "bash: cd: too many arguments" or "zsh: cd: too many arguments" generated in the command shell as you type in cd to enter a directory you want.
Feel no worry, please. This article is going to explain why cd: too many arguments occur and how to fix zsh/bash cd: too many arguments with 5 handy fixes.
Table of Contents:
- 1. Why does the Zsh/Bash: cd: too many arguments error occur?
- 2. How to fix bash: cd: too many arguments and zsh: cd: string not in pwd:?
- 3. Bonus tip: how to make the best use of cd?
Why does the Zsh/Bash: cd: too many arguments error occur?
As usual, the command shell should open the directory directly as you input the cd command line followed by the file path. It accepts a directory name as an argument and replaces the current shell environment's working directory with the given directory.
If no argument is given, the cd command behaves as if the directory named in the HOME environment variable was specified as the argument. But when you input a directory whose name contains spaces or special characters in the Unix shell, it identifies them as more than one argument. That's why it throws the error code cd: too many arguments in your bash command shell.
Note: If you're running the zsh command shell, you might get the error "cd: string not in pwd:" when you try to cd into a path that contains spaces.
Some people wonder if can Unix Shell go to more directories at a time, the answer is absolutely no. If you want to enter more directories, you have to run the cd command one by one.
If this post dispels your doubts about why too many arguments occur, please click the Share button below to help more people!
How to fix bash: cd: too many arguments and zsh: cd: string not in pwd:?
As discussed above, the Unix Shell recognizes there is more than one argument in your directory with special characters and spaces, so it failed to switch to the directory. Facing such a case, you're recommended to keep reading and this article will present you with four easy solutions to get make cd function properly.
Solution 1: Wrap the name of the directory in double quotes
One easy solution to make the cd command switch to the specialized directory successfully is to wrap the directory name in double quotes.
Step 1: Launch Terminal on your computer.
Step 2: Type in cd and then type in the double quotes symbol.
Step 3: Enter the file path containing spaces or special characters in the double quotes.
cd "my new folder"
Step 4: Hit the Enter/Return key to run the cd command line and then you can see that the directory changes to the one you input, where you're allowed to make any further actions in the new directory.
Besides, you can wrap an entire path to the directory or only wrap the path components containing spaces or special characters in the double quotes.
Solution 2: Use a backslash to avoid spaces
If there are any spaces in the directory, the cd command line in Unix Shell is going to divide them into two or more arguments. Therefore, a backslash comes here to escape the spaces, which makes the command shell treat the spaces as a literal space characters but not a separator between arguments.
Step 1: Run Terminal on your machine.
Step 2: Type in cd and follow the directory you plan to switch.
Step 3: Add backslashes in the spaces of the directory name. Please note that a forward slash works as a separator between the path components and the backslash functions as a space-escaping toolset.
cd desktop/my\ new\ folder
Step 4: Hit the Return/Enter key to make the cd bring you to a new directory.
Solution 3: Use the Tab key for auto-completion
In the command line program of Windows, Linux, and macOS, you can automatically complete given commands with the tab key. For example, in the Unix command prompt, this could be a good way to switch to a different directory more quickly.
Step 1: Open the command shell on your device.
Step 2: Start typing the path you intend to go.
Step 3: Press the Tab key on your keyboard to make use of its auto-completion function straightforwardly.
If you have several folders with similar names, you have to type the first few characters of the name to identify the one that you want to cd into. If the auto-completion fails to work, it indicates that the specified path doesn't exist.
Solution 4: Open your terminal in the destination directory
Since you aim to open the directory in the command shell for further business, do not stuck at entering cd, please. Opening a terminal in the destination directory also has the same effect.
If you're running Linux, read the stepwise instruction below please:
Step 1: Open the directory in the explorer.
Step 2: Right-click and select Open in Terminal and then you can back to the command shell to proceed.
If you're running a macOS device, you can follow these steps elaborated down:
Step 1: Open a Finder window and navigate to the folder. If you don't see the path bar at the bottom of the Finder window, choose View -> Show Path Bar.
Step 2: Right-click the folder directory you want to enter and choose New Terminal at Folder.
Then you can see that the Terminal switches to the directory you perform.
Solution 5: Drag the folder to the terminal window
If all the solutions mentioned above are not to your liking, the final solution that is worth a try must be simply dragging the folder directory to your terminal window. Just launch the terminal and left-click and drag the folder directory to the window, then you'll be in the new directory path.
If you have solved the bash: cd: too many arguments/zsh: cd: string not in pwd: successfully, why not share this article to help more people?
Bonus tip: how to make the best use of cd?
If you're a beginner to command shell or who have great passion and curiosity for terminals, various cd command lines discussed in this article must be helpful. Just have a look real quick here:
- cd:cd by itself or cd ~ will always put the user in their home directory.
- cd . : cd . will leave the user in the same directory they are currently in (i.e. the current directory won't change). This can be useful if the user's shell's internal code can't deal with the directory they are in being recreated. Running cd . will place their shell in the recreated directory.
- cd ~username: cd ~ username will put the user in the username's home directory.
- cd dir (without a /): cd dir will put the user in a subdirectory; for example, if they are in /usr, typing cd bin will put them in /usr/bin, while cd /bin puts them in /bin.
- cd ..: cd.. will move the user up one directory. So, if they are /usr/bin/tmp, cd .. moves them to /usr/bin, while cd ../.. moves them to /usr (i.e. up two levels). The user can use this indirection to access subdirectories too. So, from /usr/bin/tmp, they can use cd ../../local to go to /usr/local.
- cd -: cd - will switch the user to the previous directory. For example, if they are in /usr/bin/tmp, and go to /etc, they can type cd - to go back to /usr/bin/tmp. The user can use this to toggle back and forth between two directories without pushd and popd.
The cd: too many arguments error is not as dreadful as you imagine since this article offers 5 easy solutions to make it disappear. You can try one or more as you like to enter the relative directory by using cd. On top of that, you can find wonderful context teaching you how to use the cd command line better covered in this article.