[bash] How to cd into a directory with space in the name?

I'm attempting to get into the directory /cygdrive/c/Users/my dir/Documents:

$ DOCS="/cygdrive/c/Users/my\ dir/Documents"

$ echo $DOCS
/cygdrive/c/Users/my\ dir/Documents

$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my\: No such file or directory

$ cd /cygdrive/c/Users/my\ dir/Documents
(success)

When I manually type it in, the backspace does its escape character thing, but not when I use parameter expansion with the variable DOCS.

I tried other variations such as no backslash.

$ DOCS=/cygdrive/c/Users\ dir/Documents

$ echo $DOCS
/cygdrive/c/Users/my dir/Documents

$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my: No such file or directory

or

$ DOCS="/cygdrive/c/Users/my dir/Documents"

$ echo $DOCS
/cygdrive/c/Users/my dir/Documents

$ cd $DOCS
-bash: cd: /cygdrive/c/Users/my: No such file or directory

The same happens for $HOME:

$ echo $HOME
/home/my dir

cd $HOME doesn't work either. Quotes must be put around it.

What the heck:

$ DOCS="\"/cygdrive/c/Users/my dir/Documents\""

$ echo $DOCS
"/cygdrive/c/Users/my dir/Documents"

$ cd $DOCS
-bash: cd: "/cygdrive/c/Users/my: No such file or directory

This question is related to bash cygwin cd

The answer is


If you want to move from c:\ and you want to go to c:\Documents and settings, write on console: c:\Documents\[space]+tab and cygwin will autocomplete it as c:\Documents\ and\ settings/


$ DOCS="/cygdrive/c/Users/my\ dir/Documents"

Here's your first problem. This puts an actual backslash character into $DOCS, as you can see by running this command:

$ echo "$DOCS"
/cygdrive/c/Users/my\ `

When defining DOCS, you do need to escape the space character. You can quote the string (using either single or double quotes) or you can escape just the space character with a backslash. You can't do both. (On most Unix-like systems, you can have a backslash in a file or directory name, though it's not a good idea. On Cygwin or Windows, \ is a directory delimiter. But I'm going to assume the actual name of the directory is my dir, not my\ dir.)

$ cd $DOCS

This passes two arguments to cd. The first is cygdrive/c/Users/my\, and the second is dir/Documents. It happens that cd quietly ignores all but its first argument, which explains the error message:

-bash: cd: /cygdrive/c/Users/my\: No such file or directory

To set $DOCS to the name of your Documents directory, do any one of these:

$ DOCS="/cygdrive/c/Users/my dir/Documents"
$ DOCS='/cygdrive/c/Users/my dir/Documents'
$ DOCS=/cygdrive/c/Users/my\ dir/Documents

Once you've done that, to change to your Documents directory, enclose the variable reference in double quotes (that's a good idea for any variable reference in bash, unless you're sure the value doesn't have any funny characters):

$ cd "$DOCS"

You might also consider giving that directory a name without any spaces in it -- though that can be hard to do in general on Windows.


Why not put the following in your .cshrc (or .bashrc, or whatever your default shell is):

alias mydoc 'cd "/cygdrive/c/Users/my dir/Documents"'

First time you do this, you have to do

source .cshrc

to update the shell with this new alias, then you can type

mydoc

anytime you want to cd to your directory.

Laziness is the mother of invention...


Use quotes! cd "Name of Directory"
Or you can go to the file explorer and click "copy path" in the top left corner!


METHOD1: With Quotes

cd "C:/Prgram Files (x86)"

cd 'C:/Program Files (x86)'

Generalised

cd 'Folder Path'

Method2: Without using Quotes

cd Program\ Files \(x86\)

Generalised Whenever we want to skip next character we use blackslash \.

For the above question: cd /cygdrive/c/Users/my\ dir/Documents


Instead of:

DOCS="/cygdrive/c/Users/my\ dir/Documents"

Try:

DOCS="/cygdrive/c/Users/my dir/Documents" 

This should work on any POSIX system.


Cygwin has issue recognizing space in between the PC name. So to solve this, you have to use "\" after the first word then include the space, then the last name.

such as ".../my\ dir/"

$ cd /cygdrive/c/Users/my\ dir/Documents

Another interesting and simple way to do it, is to put the directory in quotation marks ("")

e.g run it as follows:

$ cd c:
$ cd Users
$ cd "my dir"
$ cd Documents

Hope it works?


SOLUTION:

cd "Documents and Photos"

problem solved.

The reason I'm submitting this answer is you'll find that StackOverflow is being used by every day users (not just web devs, programmers or power users) and this was the number one result for a simple Windows user question on Google.

People are becoming more tech-savvy, but aren't necessarily familiar with command line in the cases above.


Use the backslash symbol to escape the space

C:\> cd my folder

will be

 C:\> cd my\folder 

To change to a directory with spaces on the name you just have to type like this:

cd My\ Documents

Hit enter and you will be good


try

DOCS="/cygdrive/c/Users/my\ dir/Documents";

cd "$DOCS"

ok i spent some frustrating time with this problem too. My little guide.

Open desktop for example. If you didnt switch your disc in cmd, type:

cd desktop

Now if you want to display subfolders:

cd, make 1 spacebar, and press tab 2 times

Now if you want to enter directory/file with SPACE IN NAME. Lets open some file name f.g., to open it we need to type:

cd file\ name

p.s. notice this space after slash :)


As an alternative to using quotes, for a directory you want to go to often, you could use the cdable_vars shell option:

shopt -s cdable_vars
docs='/cygdrive/c/Users/my dir/Documents'

Now, to change into that directory from anywhere, you can use

cd docs

and the shell will indicate which directory it changed to:

$ cd docs
/cygdrive/c/Users/my dir/Documents

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to cygwin

How to install MinGW-w64 and MSYS2? Split text file into smaller multiple text file using command line fatal: early EOF fatal: index-pack failed Git Extensions: Win32 error 487: Couldn't reserve space for cygwin's heap, Win32 error 0 How to cd into a directory with space in the name? Git push hangs when pushing to Github? Running a shell script through Cygwin on Windows Running Git through Cygwin from Windows .ssh directory not being created Cygwin - Makefile-error: recipe for target `main.o' failed

Examples related to cd

Change directory command in Docker? Change working directory in my current shell context when running Node script How to cd into a directory with space in the name? How do I find the current directory of a batch file, and then use it for the path? Run cmd commands through Java Equivalent of shell 'cd' command to change the working directory?