Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Spaces in file and directory names UNIX
bullroarer asks:

"How do I perform Terminal commands on any items on my Mac that have spaces in their pathnames? Because Finder allows spaces in file and directory names, I'm often creating such with spaces but then if I try to cd to one in the Terminal the space kills the command. How is this done?"

There are three ways that I know of to handle this. They are:
  1. Drag-and-drop the file or directory onto the terminal; this will preserve the spaces.

  2. Enclose the path name in single quotes, like this:
    cd '/Users/username/temp/directory with spaces'
  3. Quote the space character with a backslash, like this:
    cd /Users/username/temp/directory\ with\ spaces
Any of these will allow you to easily navigate files and directories with spaces in their names.
  • Currently 2.33 / 5
  You rated: 5 / 5 (3 votes cast)

Spaces in file and directory names | 4 comments | Create New Account
Click here to return to the 'Spaces in file and directory names' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Authored by: tapella on Mar 06, '01 12:04:09PM

Your default shell should be set up for tab-completion, which makes typing these ghastly escape sequences easier (i.e., helps to make all those backslashes).
Tab-completion is really useful anywhere in the shell though... all it means is that when you've typed part of a word and then push 'tab', the shell will try to complete the rest of the word in an intelligent way. For example, let's say you have a directory with the files:
anotherFile with Spaces in THe NAME

Let's say you want to use the 'less' command to view the contents of aFile. You can type...
less aFile
less aF[tab]

In the 2nd case, after typing those two unique letters and tabbing, the rest of the file name will be completed.

If you want to do something with that 2nd file with the scary name, it's easy to just type:
less an[tab]

And it will fill in all the junk necessary for you.

For the 3rd and 4th files, since their names are so similar you really just need to type out the full names...

I hope this is news to someone ;)

[ Reply to This | # ]
tab-completion with directories
Authored by: tapella on Mar 06, '01 12:10:15PM

Just realized that I forgot to mention how this works with directory navigation. Here's an example:

to do this properly:
cd /Users/username/temp/directory with spaces

you could type this:
cd /U[tab]/use[tab]/temp/dir[tab]

The tab positions are completely arbitrary. As long as what you've typed is unique the shell will know what to fill in. The win here really comes with the last directory name.

You have to complete each directory name in the path. It will complete the name of a directory in the path you've specified so far. (ie. cd /U[tab] will complete to cd /Users)

[ Reply to This | # ]
I always use ¥
Authored by: jonahlee on Mar 07, '01 11:48:50AM

I have always just put the * symbol in place of spaces and that has worked fine for me. That way You can use directories with spaces in them.

[ Reply to This | # ]
I always use ¥
Authored by: Groucho on May 08, '01 01:57:29AM

I have no idea why I tried this, but when I ran into the space problem, I just surrounded the title in question with double quotes, as in CD /Users/unohoo/"My files" and it worked fine repeatedly.

[ Reply to This | # ]