10

I have folder name with a space between words. I use a command cd that I can to move in a need folder. How can I write space in Terminal on OS X? I tried following

MacBook-Pro:TestDrop Alexsander$ cd October Fall
-bash: cd: October: No such file or directory
MacBook-Pro:TestDrop Alexsander$ cd OctoberFall
-bash: cd: OctoberFall: No such file or directory
MacBook-Pro:TestDrop Alexsander$ cd October/ Fall
-bash: cd: October/: No such file or directory
MacBook-Pro:TestDrop Alexsander$ cd October/Fall
-bash: cd: October/Fall: No such file or directory
MacBook-Pro:TestDrop Alexsander$ cd October-Fall
-bash: cd: October-Fall: No such file or directory
MacBook-Pro:TestDrop Alexsander$ cd October_Fall
  • Perhaps loop thru with tabulator key ;) – Kyslik Oct 3 '15 at 21:05
  • cd October<TAB>. – Bakuriu Oct 4 '15 at 7:32
29

The reason it does not work is because the shell splits arguments on whitespace and therefore thinks that you call cd with the two arguments October and Fall. Escape the whitespace with \ like this:

cd October\ Fall

Another way is putting it in single or double quotes like this

cd 'October Fall'
cd "October Fall"

Or just drag and drop the folder into the Terminal.

But why don't you just type a few characters and press Tab for auto-completion?

  • 2
    you should use backslash, not slash – phuclv Oct 3 '15 at 15:20
  • Yes, I understood you. I could make it. – Alexander Khitev Oct 3 '15 at 15:24
10

The shell performs word splitting on arguments before passing it them to the command you specify. This means it takes the line (or, in some cases, lines) and splits it apart into several words, by default at spaces, tabs, and newlines.

As Lưu Vĩnh Phúc mentioned, backslashes and single quotes can be used to prevent word splitting on single characters or everything between them, respectively. Double quotes also work:

cd "October Fall"

In this particular case, ' and " behave identically, but they do have differences.

' disables all special character interpretation (even \), but cannot contain a '. " only disables word splitting and file name expansion (file name expansion is things like ls *.mp3 to list all files with names ending in .mp3. The * triggers the file name expansion here). Since " allows some argument expansions, it's useful if, for example, you have a file name with a space in it inside a variable:

$ filename=October\ Fall
$ #               ^-- Still need a backslash or quoting to prevent word 
$ #                   splitting here, but the backslash/quotes will not be stored
$ cd $filename
bash: cd: October: No such file or directory
$ cd '$filename'
bash: cd: $filename: No such file or directory
$ cd "$filename"
$ pwd
/path/to/October Fall

Note that ' and " do not (by default) cause word splitting themselves, so this, despite being messy, would work for you:

cd "Oct"'ober F'"all"

This is useful if you've got something such as a directory named Octobers' $HOME. The ' in it would break a single pair of ' quoting, and inside " quotes, $HOME will expand to your home directory. But any of these combinations (and many others) will work safely:

cd Octobers\'\ \$HOME
cd "Octobers' "'$HOME'
cd "Octobers' "\$HOME
cd "Octobers' \$HOME"

Something else to keep in mind is that many modern shells offer tab completion. Both Bash and Zsh (probably Ksh as well, but I haven't tested it) can expand part of a directory's name to its full name, complete with safe escaping via \s. So cd Octobers<tab> becomes cd Octobers\'\ \$HOME. You may have to press tab multiple times if there are multiple matches. Also, your shell may not have tab completion enabled by default, check your shell's documentation.

  • 1
    TMI but still good to know. +1 – Josiah Oct 3 '15 at 22:28
  • @Josiah Explaining why the asker gets the result that they do in addition to how to get the result they want isn't too much information. – Blacklight Shining Oct 31 '15 at 0:20
0

If your folder or file is named like "Autosave Information" which contains "space" or any special character.

>> cd Autosave\ Information

you can use special characters with the help of "\" or you can tell the terminal that the name you are giving is a String.

>> cd "Autosave Information"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.