Moving around in Unix

A crucial command in Unix is the ability to Change Directory (that is, move to a different folder). The Change Directory command is:

cd

 

When Terminal opens, it opens in your home folder defaults to what’s called your home folder. On mac, this is usually /Users/yourusername/

 

To get to your Desktop, which is a folder in your home folder, you would type

cd Desktop

and then you would be in your Desktop folder. Note that Unix is caps sensitive, so

cd desktop

would just result in the Mac error beep.

 

Names with spaces in them are annoying.  For this reason, I try and not ever use a space in a file or folder name.  I usually go with underscores (‘_’) but a hyphen (‘-‘) is also widely used in place of spaces.

I do have a folder on my Desktop named Lab safety.  The unix name for this is:

Lab\ safety/

Note you need a ‘\’ before the space.  This is why spaces are bad.

To navigate up one level type

cd ..

and then you’d be back in your home folder.

 

Typing Desktop out each time (and every other folder name in full) is both annoying and prone to error. Fortunately, Unix has something called tab completion. You can type a partial name of a file or folder, and hit Tab, and the name will be filled in.

 

Typing

cd Desk<tab>

will complete to

cd Desktop

 

Now, it only goes to the next unique character. Typing

cd D<tab>

brings up nothing. However, hitting tab twice will bring up all the possibilities.

cd D<tab><tab>

will likely produce

Desktop/ Documents Downloads/

and perhaps a couple of other options, depending on what you have in your home folder. (As an aside, the / after Desktop and Downloads are an indication of a folder, and not a file.)  If you ever want to see everything in a folder, you can either hit tab twice, or type the LiSt command

ls

 

Your home directory is special, and you can always return to it by typing

cd ~

 

You can also always navigate directly to a folder by typing put the full path

cd /U<tab>/<username>/Desk<tab>

should always bring you to your Desktop folder.

 

Similarly, you can travel down multiple directories at once. If, for some reason, you were in the /Users/ directory, you could type

cd <username>/Desk<tab>

to get to your Desktop folder again. Also, yes, you can tab complete multiple directories deep.

 

If you ever want to know what directory you are in type:

pwd

to Print the Working Directory

Go to Top