Cannot change directory in Terminal if target has spaces in it

bashterminal

I plugged my external HD, then in a Terminal, I type

ls /Volumes/TOSHIBA\ EXT

This lists files and folders as expected, but then entering:

cd /Volumes/TOSHIBA\ EXT

throws the following message

-bash: cd: /Volumes/TOSHIBA\: No such file or directory

I can even reproduce this behavior with my own folders.

What is going on here?
How do I change to a directory when it has spaces?

this is my environment context

uname -vsr
Darwin 17.5.0 Darwin Kernel Version 17.5.0: Mon Mar 5 22:24:32 PST 2018; root:xnu-4570.51.1~1/RELEASE_X86_64

Best Answer

Spaces in file names can be a hassle in the shell. Your best solution would be to both wrap the name in " characters and use the shell's built in completion to get the right name.

Type cd "/Volumes/TOSH then type a tab and the shell will complete the name with any spaces and non-standard characters included. If the tab completes the entire path then the shell will even insert a "/ at the end so you are ready to then hit return.

Note that standard bash completion will work on file paths and names for most shell commands, not just cd.