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

BashCygwinCd

Bash Problem Overview


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

Bash Solutions


Solution 1 - Bash

$ cd "$DOCS"

You need to quote "$DOCS" to prevent spaces from being parsed as word separators. More often than not, variable references should be quoted.

Note that $HOME would have the same problem. The issue is coming from when the shell evaluates variable references; it's nothing to do with what variables you use or how you assign to them. It's the expansion that needs to be quoted.

$ echo $HOME
/home/my dir

This is deceptive. echo is actually echoing the two strings /home/my and dir. If you use cd or ls you'll see how it's actually working.

$ ls $HOME
ls: cannot access /home/my: No such file or directory
ls: cannot access dir: No such file or directory
$ cd $HOME
bash: cd: /home/my: No such file or directory
$ cd "$HOME"
<success!>

> Can I ask why it works when I manually type it in but not in a variable?

Great question! Let's examine the commands you typed:

$ 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 reason this doesn't work is because Bash doesn't parse quotes inside variable expansions. It does perform word splitting, so whitespace in unquoted variable expansions is taken as word separators. It doesn't parse quotes in any way, meaning you can't put double quotes inside a variable to override word splitting.

$ cd $DOCS

Because of this, cd is passed two parameters. As far as cd knows it looks like you wrote:

$ cd '"/cygdrive/c/Users/my' 'dir/Documents"'

Two parameters, with double quotes intact.

Solution 2 - Bash

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.

Solution 3 - Bash

$ 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.

Solution 4 - Bash

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

Solution 5 - Bash

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

Solution 6 - Bash

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...

Solution 7 - Bash

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/

Solution 8 - Bash

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 9 - Bash

try

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

cd "$DOCS"

Solution 10 - Bash

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 :)

Solution 11 - Bash

Use the backslash symbol (\) to escape the space:

C:\> cd my folder

will be

 C:\> cd my\ folder 

Solution 12 - Bash

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

Solution 13 - Bash

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

Solution 14 - Bash

The approaches discussed here don't work for aliases in a consistent basis - adding/not adding quotes etc.

E.g. if I have a directory variable like dir_with_spaces='path of the directory with spaces', it is not possible to directly use alias new_dir='cd $dir_with_spaces/subdirectory'.

As an alternative, it would be useful to create a custom function that adds quotes around the cd, and then use this custom function instead.

cdwq() # Custom cd to support directories with spaces in the name by adding quotes around it
{
 local folder_path=$1
 cd "${folder_path}"
}

Now, it is possible to use this custom function with any directory variable.

dir_with_spaces='path of the directory with spaces'
dir_without_spaces='path_of_the_directory_without_spaces'

alias new_dir1='cdwq $dir_with_spaces/subdirectory1'
alias new_dir2='cdwq $dir_with_spaces/subdirectory2'
alias new_dir3='cdwq $dir_without_spaces/subdirectory3'
alias new_dir4='cdwq $dir_without_spaces/subdirectory4'
alias new_dir5='cdwq $dir_with_spaces/subdirectory5'

Solution 15 - Bash

If there is space inside dir name, you can use wild card character (? or *) in place of space & wrap the directory name in double-quotes.

Example - in the below path, "Program Files" has space inside -

/mnt/c/Program Files$ JAVA_HOME=/mnt/c/"Program?Files"/Java/jdk-11.0.12

/mnt/c/Program Files$ cd $JAVA_HOME

/mnt/c/Program Files/Java/jdk-11.0.12$

Solution 16 - Bash

Instead of:

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

Try:

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

This should work on any POSIX system.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2316667View Question on Stackoverflow
Solution 1 - BashJohn KugelmanView Answer on Stackoverflow
Solution 2 - BashMindsect TeamView Answer on Stackoverflow
Solution 3 - BashKeith ThompsonView Answer on Stackoverflow
Solution 4 - BashNsaavsView Answer on Stackoverflow
Solution 5 - BashsamarmohanView Answer on Stackoverflow
Solution 6 - BashFlorisView Answer on Stackoverflow
Solution 7 - BashantonioView Answer on Stackoverflow
Solution 8 - BashDamilare OyediranView Answer on Stackoverflow
Solution 9 - Bashmichael501View Answer on Stackoverflow
Solution 10 - BashVasyl GutnykView Answer on Stackoverflow
Solution 11 - BashIlemona James AtulukuView Answer on Stackoverflow
Solution 12 - BashShaurya UppalView Answer on Stackoverflow
Solution 13 - BashBenjamin W.View Answer on Stackoverflow
Solution 14 - BashrisjainView Answer on Stackoverflow
Solution 15 - BashSumit BurnwalView Answer on Stackoverflow
Solution 16 - Bashuser2697927View Answer on Stackoverflow