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

前端 未结 14 2161
独厮守ぢ
独厮守ぢ 2020-11-27 05:30

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

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

$ echo $DOCS
/cyg         


        
相关标签:
14条回答
  • 2020-11-27 05:54
    $ 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.

    0 讨论(0)
  • 2020-11-27 05:55
    $ 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.

    0 讨论(0)
  • 2020-11-27 05:55

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

    0 讨论(0)
  • 2020-11-27 05:56

    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

    0 讨论(0)
  • 2020-11-27 05:58

    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

    0 讨论(0)
  • 2020-11-27 06:02

    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.

    0 讨论(0)
提交回复
热议问题