Changing folders in the Terminal on Mac OS X

Demonstrating the different ways to change folders

I think one of the great things about Mac OS X is it lets people learn Unix commands in a familiar setting, and I’ve been encouraging people to do so. When you mess around in Terminal.app, almost all the commands work the same in the other BSDs, and in a similar fashion to Linux, Solaris etc.

One of the most common questions I’m asked by people learning Unix commands from scratch on the Mac is how to change folders/directories that have spaces or punctuation characters.

There are three ways, the simplest of which is just to enclose the name of the directory in double quotes:

% cd "/Users/rubenerd/Folder With Spaces"

An alternative which you may be more familar with if you’ve done scripting in Perl for example is to "escape" the characters with backslashes:

% cd /Users/rubenerd/Folder With Spaces

The third way is to escape the characters in the directory name, but let the computer do it for you: the default shells in Mac OS X allow tab filename completion. In this case, start typing the name of the directory you want to change to, then hit [TAB].

% cd /Users/rubenerd/Fold[TAB]
becomes…
% cd /Users/rubenerd/Folder With Spaces

Icon from the Tango Desktop projectIf you have several directories with the same first few characters the shell will either beep at you or show you a list of the folders (and sometimes files, symbolic links etc) that match your criteria. In this case, just keep filling the name of the directory until you have enough of it that’s unique.

These three methods are also used if you need to reference files with spaces and other characters in the Terminal too. Personally since I’ve started living in Terminals I choose not to name my files with spaces any more to save myself some time, but if you’re mostly a graphical desktop user that’s dabbling in this stuff, these work just fine.


Imprint

This is one of about 5000 posts on Rubénerd. View the home page for the latest, or related posts also tagged with:

If you liked this post, feel free to buy me a coffee, leave me a comment on Twitter, or email me at weblog2017@rubenschade.com. Thanks :).