Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

cd

cygwin

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 
like image 878
user2316667 Avatar asked Aug 19 '13 21:08

user2316667


People also ask

How do you cd files name with spaces?

If you take a folder and drag-and-drop it onto the Command Prompt, it will fill the window with the absolute pathname of that folder. Therefore, to quickly move the Command Prompt to a a specific folder, do the following: Type cd followed by a space. Drag and drop the folder on to the Command Prompt.

How do you go to a directory with a space in Linux?

The Linux terminal treated files and folders differently that have spaces in their names. So this article solved the problem. If we want to name a file or directory with spaces we can do it by using apostrophes ( ' ' ) , quotation marks (“ ”) or escape sequence ( \ ).

How do I cd a directory with special characters?

If you can see the directory and if you want to access it using terminal, just type: cd first and then drag and drop the directory on the terminal and hit enter .

How do you cd into a directory?

To navigate into the root directory, use "cd /" To navigate to your home directory, use "cd" or "cd ~" To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -"


2 Answers

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

like image 82
John Kugelman Avatar answered Sep 20 '22 15:09

John Kugelman


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.

like image 22
Mindsect Team Avatar answered Sep 22 '22 15:09

Mindsect Team