DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Forums
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OS X | UNIX

back to LC

Mac OS X Unix Tutorial

Part 1 - Lose the Mouse (page 2 of 2)

Introducing 'cd'

This command changes your current working directory. Just give it the pathname of the directory you wish to move to. The pathname can be either absolute or relative.

For example:

% cd /Users

will set the current working directory to the Users directory just above your home directory.

Confirm this with:

% pwd
% ls

To get back to your home directory, type either of:

% cd /Users/your-home
or
% cd ~
or
% cd

The '~' or tilde character is shorthand for your home directory. Typing just 'cd' will also get back to your home directory. This works on the default Mac OS X set-up, but not universally.

To move to the Documents directory within your home, follow the above command with:

% cd Documents

'Documents' is a relative pathname and assumes your current working directory is (soon to become 'was') your home.

% cd /Users/your-home/Documents
% cd ~/Documents

will do the same using absolute pathnames.

'ls' - Feel the Power

You will be aware of the Finder's ability to show file information like size, date of change, and permissions. Well, 'ls' can do all that too. We have to specify options to tell 'ls' how to behave.

Try:

% ls -l
% ls -t
% ls -lt

on a number of different directories.

'-l' means 'long', and tells 'ls' to output additional information. As an example of the information output:

-rw-r--r-- 1 melkor staff 17181 Apr 23 20:04 ipfw.txt
drwxr-xr-x 4 melkor staff   264 Mar 10 18:08 Public

shows a file 'ipfw.txt', and a directory 'Public'.

Considering 'ipfw.txt' in more detail:

-rw-r--r--

The initial '-' indicates that 'ipfw.txt' is a file, directories have 'd'. The next nine characters give the permissions - who can read, write, and execute the file, in the order user, group, and others. We will discuss permissions in more detail in a later column. (Also see the Advanced Unix Lesson.) '1' is the number of links (ignore this).

melkor staff

gives the user who owns the file, and the group which owns the file.

The next fields are the file size in bytes, the date and time of last modification, and finally the filename itself. Note that the size of a directory does not include the total size of all files within the directory.

The option '-t' sorts by time modified (most recently modified first) before sorting the operands by lexicographical order.

And finally, '-lt' does both.

Auto Completion

Of immense use is auto completion. By hitting the tab key as you are partway through typing a pathname or file name, one of three things will happen:

  1. the next part of the path will be automatically filled in for you, if what you have typed so far is unambiguous
  2. a list of possibilities is displayed if it is ambiguous
  3. nothing, if it does not represent a path that exists.

Try:

% ls /U
  then-hit-tab
  then hit tab again.

Auto completion also works when you are partway through typing a command.

 
Tell Me More...

Tilde

On my (UK) keyboard, the '~' character is just to the left of 'Z'. It is equivalent to:

/Users/your-home

and is an absolute pathname.

It can be used with all commands, for example:

% ls ~

will list your home directory.

Command Operands

One can supply additional information to Unix commands. 'ls' can take a directory to list. Each piece of additional information is termed an operand.

Command Options

Options are like operands except they change the default way in which a command operates. They are (usually) placed before operands and are distinguished by a preceding '-'.

Options are one letter long, and case is important. '-a' is different to '-A'. To save typing, multiple options may be merged, and preceded by a single '-'.

For example, the following are equivalent.

% ls -a -l -t
% ls -alt
% ls -al -t

Finally, '-h' is often the help option. Try:

ls -h

Ok, so not that helpful :-)

'man' - Tell Me More

'man' is a very useful command. It calls upon the Unix manual pages. So:

% man ls

will tell you all about 'ls'. You can't do much harm with 'ls', so read the manual and have a play. 'man' is very much a reference, not a tutorial - as you are about to find out. :-)

Hit the 'return' key to scroll down a line at a time, or 'space' to scroll down a page at a time. Hit 'q' to quit out of the manual.

In Part Two

In the next part to this column I will cover more useful Unix commands that allow you to create and delete files and directories.

In the meantime, try the following commands:

% cd ~
% ls ..
% ls .
% ls -al

When you wish to finish working at the command line close the Terminal window by typing:

% logout

Enjoy :-)


Discuss this article in the Learning Center forum




previous

Part 1 - Lose the Mouse (page 2 of 2)

next

Copyright © 2000-2008 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.