Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
I forgot how to change directories when the directory name or file name is more than one word with a space between the words. For example, when I type "cd Pictures" then use the "ls" command to see the folders or files there is a directory My Pictures ( I know it's a directory because I used "ls -all" ) but when I try to use the command "cd My Pictures" then the return messages is "no such file or directory". What did I forget?
 

satcomer

macrumors G3
Feb 19, 2008
8,742
1,845
The Finger Lakes Region
To change connection settings via Terminal use networksetup instead of the older ifo. Just do a man on networksetup.

If you are unsure on how to get to a folder just drag the folder to the Terminal window and release and then the path to that folder! This way you can see how to make a manual path!
 

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
Remember you can press tab for auto complete

And you're looking for a backslash before the space

My\ Pictures, or "My Pictures" with quotes.

well, let's say you wanted to go to that directory directly without using the 'cd' command to switch directories one by one. I tried the command 'cd \Pictures\My\ Pictures' but it didn't work
[doublepost=1465860642][/doublepost]never mind I see it's 'cd ~/Pictures/My\ Pictures'
 

mfram

Contributor
Jan 23, 2010
1,223
289
San Diego, CA USA
It would be one of the following:
Code:
cd Pictures/My\ Pictures
cd "Pictures/My Pictures"

Forward slash ('/') is the directory separator. Normally in Unix shells, spaces are used to separate arguments. So if you want to add a space to an argument you must add the escape character ('\') before the space. That's why the backslash is there.
 

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
It would be one of the following:
Code:
cd Pictures/My\ Pictures
cd "Pictures/My Pictures"

Forward slash ('/') is the directory separator. Normally in Unix shells, spaces are used to separate arguments. So if you want to add a space to an argument you must add the escape character ('\') before the space. That's why the backslash is there.

thank you, another question is how does this usage relate to a filename that includes a parenthesis such as '(' or ')' ??? I have tried to use the backslash in a filename to determine some characteristics of the filename but the shell tells me that there is an unrecognized token near the (. However it did not seem to have any problem with the '-' character.

Thanks for your assistance.
 

mfram

Contributor
Jan 23, 2010
1,223
289
San Diego, CA USA
Yes, the parenthesis is a special character. As ar braces {} and brackets []. Normally you shouldn't have to worry since TAB-completion will help you out. As you type commands, put in the first couple of characters of a file and press TAB. The shell will complete the rest of the filename as best it can. If the filename includes special characters, the shell will automatically insert the escape characters.

Brace and bracket usage shows below:

Code:
host:~$ ls pic*
pic1.jpg  pic2.jpg  pic3.jpg  pic4.jpg  pic5.jpg
host:~$ ls pic[123].jpg
pic1.jpg  pic2.jpg  pic3.jpg
host:~$ ls pic{2,4}.jpg
pic2.jpg  pic4.jpg
host:~$ mv pic{2,7}.jpg
host:~$ ls pic*  
pic1.jpg  pic3.jpg  pic4.jpg  pic5.jpg  pic7.jpg
host:~$ mv pic{7,2}.jpg
host:~$ ls pic*  
pic1.jpg  pic2.jpg  pic3.jpg  pic4.jpg  pic5.jpg
 

chown33

Moderator
Staff member
Aug 9, 2009
9,891
6,872
Beyond the pale
thank you, another question is how does this usage relate to a filename that includes a parenthesis such as '(' or ')' ??? I have tried to use the backslash in a filename to determine some characteristics of the filename but the shell tells me that there is an unrecognized token near the (. However it did not seem to have any problem with the '-' character.

Thanks for your assistance.
When you ask a question about a command line, please provide the exact command-line used, and the exact text of any error messages. You can select text in a Terminal window, then copy it to the clipboard and paste it into a post. It's difficult to guess exactly what you may have typed when all you provide is a vague description.

If you want to see what you should type for any pathname, you can use the 'echo' command, then a space, then drag and drop the desired file from the Finder onto the Terminal window. The full path will then be automatically typed in, with the necessary \'s in the required places.

The 'echo' command simply echoes its args, but it does so after the \'s and quotes have been removed, wildcards expanded, etc. So you can simply press RETURN at that point to see what the actual filename will be.

The process of using filenames with special characters for the shell is called "quoting". There's a whole section on it in the 'bash' man page:
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/bash.1.html

The heading for the section is QUOTING, which you can search for on that man page.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.