Glam Prestige Journal

Bright entertainment trends with youth appeal.

I have a directory called "Reader 0.5" in my Desktop on Mac Os X. When to access the directory in terminal, I am using below code:

cd /Users/niho/Desktop/Reader 0.5

but it throws:

No such file or directory

error.

How can I cd into that directory?

Thanks.

0

5 Answers

Either you put quotes around the directory name (cd "/Users/niho/Desktop/Reader 0.5") or you escape the directory name (/Users/niho/Desktop/Reader\ 0.5).

3

You can escape the space:

cd /Users/niho/Desktop/Reader\ 0.5

Fyi, using the Tab in bash shortcut would break at the first space it encounters if multiple directories have identical first names. In such cases a user would have to use:

cd Adobe\ Creative\ Cloud/

or what I prefer,

cd 'Adobe Creative Cloud'
1

As others have mentioned, quoting the path or backslash-escaping the spaces will work.

In addition bash, the default shell on Mac OS X, supports command-line completion using the Tab key. So e.g. if you type:

cd /Users/niho/Desktop/Re

then press the Tab key, the shell will fill in the rest of the folder name (as long as there are no other folders on your Desktop starting with "Re"), and will take care of quoting the arguments to cd if there are spaces in the directory name it fills in.

Here's a more comfortable way if you want use the cd commands to certain directories more often. It avoids writing the directory name every time.

In your .bashrc or .profile, insert:

# activate cdable_vars
shopt -s cdable_vars
# define shortcut for your directory, here DIR
export DIR="/Users/<username>/path/to/your/dir"

Execute your script once: . .bashrc

Then you can cd to your directory like this:

cd DIR

This should work even if the path contains spaces.

In shell scripting, however, you must quote the variable like this:

cd "$DIR"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy