Terminal Directories with Spaces or Special Characters

I’ve been trying to reduce the size of the Time Machine backup on my Time Capsule with a nifty trick that I’ll share in the next post.  In the process I learned something very useful in dealing with Terminal commands, in particular with directories.

The name of the directory I was trying to ‘cd’ into had spaces as seen below.  I used the “ls” command to show a list of drives in /Volumes.

Image

If I simply type “cd /Andrew Cockerham’s Time Cap” it will give an error “no such file or directory”.  So to get into that directory, the easiest way is to wrap it in quotes:

Image

You can ignore the fact that I’m in ‘root’ to do this method, you do not need to be in root for it to work.

The second way to do it is using backwards slashes, ‘\’, before each space in the directory name:

Image

Note also that back slashes must be in front of the special characters, in this case the apostrophe.

Now this leads me to the next trick.  Typing out this long directory name with all the back slashes can be tedious, so a great shortcut is to simple start typing the first few letters of the directory, and then pressing the “Tab” key, and it will autocomplete the directory name for you, automatically adding the back slashes where need.

Image

and autocomplete…

Image

I love neat little keyboard shortcuts, especially in Terminal.

Hope you found it useful!

Advertisements

One thought on “Terminal Directories with Spaces or Special Characters

  1. Pingback: What I learned today | Reduce the size of Time Machine Backups

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s