DEV Community

Marmot
Marmot

Posted on

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

Hi, I'm trying to navigate my folders using terminal and want to go into a folder called 'Front end'. I googled and found 2 solutions :

-Put quotes around the directory name. (cd ~/"Front end"/)
-Escape the directory name with a backslash (cd ~/Front\ end/)

None of these solutions work for me. Am I missing something?

Thanks for help.

https://dev-to-uploads.s3.amazonaws.com/i/8fksvb5za17363gsjrgh.png

Top comments (9)

Collapse
 
brandinchiu profile image
Brandin Chiu

Unix systems will support terminal autocomplete. Start typing as much of the name as you can, and then press tab, the terminal should auto-complete the rest, which will include how to escape the space.

You can use this as your guide.

Collapse
 
codandcode profile image
Marmot

Thank you!

I have tried that but it doesn't seem to work in my terminal either.

On the other end, it looks like my problem came from somewhere else because even when I removed the space in the folder's name the problem remained but I could access the directory by entering the full path.

Is it like this that if I want to have access a directory, I always have to enter the full path even if I'm in the parent directory?

Collapse
 
brandinchiu profile image
Brandin Chiu

No, it shouldn't be.

Are you sure your "Front End" directory is relative to your home directory?

All of the cd commands in your screenshot are being applied relative to ~, which is essentially just an alias for /Users/morgan.

Thread Thread
 
codandcode profile image
Marmot

Alright so everything works now.

My mistake was that even when I tried to remove the tilde, I was still putting a forward slash before the directory name so it didn't work. It is then very possible to move from directory to directory and I'm happy :)

Thanks a thousand!

Thread Thread
 
brandinchiu profile image
Brandin Chiu

When moving through your file system, if you add the forward slash, then your system will think your path starts from your system root.

For example, say you are in your home directory at: /Users/Morgan.

If you type cd frontend, then this is equivalent to /Users/Morgan/frontend.

If instead you type cd /frontend, then this is equivalent to /frontend.

Adding the / turns your path into an "absolute path". Without it it is a "relative path".

Thread Thread
 
codandcode profile image
Marmot

And with that you just made it absolutely clear. Thanks again!

Collapse
 
codandcode profile image
Marmot

Ok, thanks. Autocomplete actually works but I do indeed have to type the full path everytime starting from the home directory.

Thanks for your help.

Collapse
 
brandinchiu profile image
Brandin Chiu

Autocomplete should work from any directory and will autocomplete file names and directory paths relative to where you currently are in your filesystem.

Collapse
 
rfaulhaber profile image
Ryan Faulhaber

Knowing about "\ " is good to know, but at least with bash you can cd to a directory using quotes, like cd "my dir with spaces". Weirdly you can also do something like cd "foo bar/baz" and bash can figure it out