You'll get your Mac news here from now on...

Help TMO Grow

Subscriber Login

Advertising Info


The Mac Observer Express Daily Newsletter


More Info

Site Navigation

Home
News
Tips
Columns & Editorials
Reviews
Reports
Archives
Search
Forums
Links
Mac Links
Software
Reports
Contact

Mac OS X Command Line 101
by Richard Burton


The Missing Argument Chapter
Part IV of this series...
May 13th, 2002

"This isn't an argument."
"Yes it is."
"No it isn't. It's just contradiction."
"No it isn't."
"Yes it is!"
- Michael Palin and John Cleese, Monty Python's Flying Circus, "The Argument Clinic"

This series is designed to help you learn more about the Mac OS X command line. If you have any questions about what you read here, check out the earlier columns, write back in the comments below, or join us in Hardcore X! forum.

[Editor's Note: This installment was originally planned to be run as Part III of this series, and should be read before the piece actually published as Part III, Understanding Metacharacters. As such, we are publishing it early this week for those working along with the series, numbered as Part IV.]

Now that we've taken a look at some basic commands, let's examine two key, related features of Unix commands: arguments and options.

We saw command arguments in the previous column. Remember the cd command?

    [localhost:~] dr_unix% pwd
    /Users/dr_unix
    [localhost:~] dr_unix% cd Sites
    [localhost:~/Sites] dr_unix% pwd
    /Users/dr_unix/Sites
    [localhost:~/Sites] dr_unix% cd
    [localhost:~] dr_unix% pwd
    /Users/dr_unix
    [localhost:~] dr_unix%
    

When we typed the command cd Sites to change our current working directory, we told the computer that 'Sites' is the target directory. 'Sites' is the argument to the cd command. Recall that, if cd is given no argument, it defaults to the user's home directory. Also, most, if not all, Unix commands will check to see if an argument is valid before running the intended operation. (And if one doesn't, it should.)

    [localhost:~] dr_unix% cd Lirbary
    Lirbary: No such file or directory.
    [localhost:~] dr_unix% 
    

Some commands are very persnickety about how many arguments they take, and will insist on the proper number. Others are more devil-may-care, and will take however many you wish. However, the behavior may vary by the number of arguments.

    [localhost:~] dr_unix% cd Music Pictures
    cd: Too many arguments.
    [localhost:~] dr_unix% 
    

Check the man page for a particular command for the details.

Take ls as an example of a command that can take any number of arguments. Recall that if you give it no arguments ls will print a list of all the files in your current working directory.

    [localhost:~] dr_unix% pwd
    /Users/dr_unix
    [localhost:~] dr_unix% ls
    Adam.txt   Documents  Movies     Pictures   Sites      test_1.txt
    Desktop    Library    Music      Public     personal   who_list
    [localhost:~] dr_unix% 
    

If, however, you pass ls an argument, the behavior changes. It the argument is a file, you get the name of the file.

    [localhost:~] dr_unix% ls who_list
    who_list
    [localhost:~] dr_unix% 
    

That isn't too useful. However, if the argument is a directory, you get a list of files in that directory.

    [localhost:~] dr_unix% ls Sites
    images     index.html
    [localhost:~] dr_unix% ls Sites/images
    apache_pb.gif   macosxlogo.gif  web_share.gif
    [localhost:~] dr_unix%
    

While that is useful, it is also a bit limited. Fortunately, ls is more powerful than we've seen so far. For one thing, it can take any number of arguments.

    [localhost:~] dr_unix% ls test_1.txt who_list
    test_1.txt who_list
    [localhost:~] dr_unix% 
    

Also, ls is clever enough that you can combine regular files with directories.

    [localhost:~] dr_unix% ls test_1.txt Library Movies
    test_1.txt
    
    Movies:
    
    Sites:
    images     index.html
    [localhost:~] dr_unix%
    

Notice how the directories are separated from the regular files, and how the directories are separated from each other. Also, notice that the Movies directory is listed, but as it contains no files, nothing is listed under it.

While lists of files are handy to know, you often want to know things like: how big are these files, when were they last changed, etc. ls provides this if you use different options.

Options are small flags that you pass to a command to slightly alter or refine its behavior. The options are denoted by starting with a '-' and come before arguments to the command start. The behavior of the options, and which ones are valid, varies by command. If you are unsure about what is available for a particular command, remember that you can always check the man page.

For ls, many options are available, but I'll use a few of the more common ones as examples. One of the most useful is the '-l' option, which gives a LOT of information.

    [localhost:~] dr_unix% ls -l
    total 24
    -rw-r--r--   1 dr_unix  staff   15 Mar 25 17:28 Adam.txt
    drwx------   7 dr_unix  staff  194 Mar 25 17:25 Desktop
    drwx------  18 dr_unix  staff  568 Mar 25 09:48 Documents
    drwx------  26 dr_unix  staff  840 Mar 17 12:42 Library
    drwx------   2 dr_unix  staff  264 Mar 21 20:30 Movies
    drwx------   2 dr_unix  staff  264 Mar 21 20:30 Music
    drwx------   6 dr_unix  staff  264 Mar 21 20:30 Pictures
    drwxr-xr-x   4 dr_unix  staff  264 Mar 22 18:06 Public
    drwxr-xr-x   4 dr_unix  staff  264 Aug 21  2001 Sites
    drwxr-xr-x   7 dr_unix  staff  264 Mar 23 11:21 personal
    -rw-r--r--   1 dr_unix  staff   15 Mar 18 10:47 test_1.txt
    -rw-r--r--   1 dr_unix  staff  243 Mar 18 10:47 who_list
    [localhost:~] dr_unix%
    

So, just what does all this mean? The first character tells us whether the file is a directory (d) or a regular file (-). (There are other possibilities, but don't worry about that for now.) The next nine characters show the permissions on each file. For each file, there are three levels of permission: read (r), write (w), and execute (x). The first three show permission for the file's owner, the second three for anyone else in the same user group, and the final three for the rest of the world. (If you aren't familiar with user groups, don't sweat it.) Next comes the number of links (again, don't sweat it), followed by the file's owner, the file's user group, the size of the file, the date it was last altered, and finally the name of the file.

Normally, ls on the Mac sorts in ASCII order, which is alphabetical by capital letters followed by alphabetical by lowercase. If you want to list in reverse order, you can use the '-r' option.

    [localhost:~] dr_unix% ls
    Adam.txt   Documents  Movies     Pictures   Sites      test_1.txt
    Desktop    Library    Music      Public     personal   who_list
    [localhost:~] dr_unix% ls -r
    who_list   personal   Public     Music      Library    Desktop
    test_1.txt Sites      Pictures   Movies     Documents  Adam.txt
    [localhost:~] dr_unix% 
    

Or if you want to sort from newest to oldest, use the '-t' option.

    [localhost:~] dr_unix% ls -t
    Adam.txt   Documents  Public     Pictures   who_list   Library
    Desktop    personal   Music      Movies     test_1.txt Sites
    [localhost:~] dr_unix%
    

What if you want to sort from oldest to newest? You can just combine the '-t' and '-r' options. This can be entered as ls -t -r. For convenience (i.e. laziness), you can combine flags, like ls -tr.

    [localhost:~] dr_unix% ls -rt
    Sites      test_1.txt Movies     Music      personal   Desktop
    Library    who_list   Pictures   Public     Documents  Adam.txt
    [localhost:~] dr_unix%
    

Or, going hog-wild,

    [localhost:~] dr_unix% ls -lrt
    total 24
    drwxr-xr-x   4 dr_unix  staff  264 Aug 21  2001 Sites
    drwx------  26 dr_unix  staff  840 Mar 17 12:42 Library
    -rw-r--r--   1 dr_unix  staff   15 Mar 18 10:47 test_1.txt
    -rw-r--r--   1 dr_unix  staff  243 Mar 18 10:47 who_list
    drwx------   2 dr_unix  staff  264 Mar 21 20:30 Movies
    drwx------   6 dr_unix  staff  264 Mar 21 20:30 Pictures
    drwx------   2 dr_unix  staff  264 Mar 21 20:30 Music
    drwxr-xr-x   4 dr_unix  staff  264 Mar 22 18:06 Public
    drwxr-xr-x   7 dr_unix  staff  264 Mar 23 11:21 personal
    drwx------  18 dr_unix  staff  568 Mar 25 09:48 Documents
    drwx------   7 dr_unix  staff  194 Mar 25 17:25 Desktop
    -rw-r--r--   1 dr_unix  staff   15 Mar 25 17:28 Adam.txt
    [localhost:~] dr_unix%
    

One other very handy option for ls is '-a'. This shows all files:

    [localhost:~] dr_unix% ls -a
    .                   .Trash              Library             personal
    ..                  .ssh                Movies              test_1.txt
    .CFUserTextEncoding .tcsh_history       Music               who_list
    .DS_Store           Adam.txt            Pictures
    .FBCIndex           Desktop             Public
    .FBCLockFolder      Documents           Sites
    [localhost:~] dr_unix% 
    

The files that start with a '.' are known as "hidden files". Normally they are files that hold your personal settings for particular applications (.mailrc for the Unix mail application, for example). Normally, you want to ignore them, so ls defaults to not printing them. And of course, '-a' can be combined with other options, and yes Virginia, hidden files can also be directories:

    [localhost:~] dr_unix% ls -al
    total 120
    drwxr-xr-x  21 dr_unix  staff    670 Mar 25 17:46 .
    drwxr-xr-t   4 root     wheel     92 Mar 13 16:54 ..
    -rw-r--r--   1 dr_unix  staff      3 Mar 17 12:38 .CFUserTextEncoding
    -rw-rw-rw-   1 dr_unix  staff   6148 Mar 25 17:46 .DS_Store
    -rw-rw-rw-   1 dr_unix  staff  32768 Mar 21 20:29 .FBCIndex
    drwxrwxrwx   3 dr_unix  staff     58 Mar 21 20:29 .FBCLockFolder
    drwx------   4 dr_unix  staff     92 Mar 25 17:25 .Trash
    drwx------   2 dr_unix  staff     24 Mar 12 18:26 .ssh
    -rw-------   1 dr_unix  staff   1919 Mar 25 17:19 .tcsh_history
    -rw-r--r--   1 dr_unix  staff     15 Mar 25 17:28 Adam.txt
    drwx------   7 dr_unix  staff    194 Mar 25 17:25 Desktop
    drwx------  18 dr_unix  staff    568 Mar 25 09:48 Documents
    drwx------  26 dr_unix  staff    840 Mar 25 17:46 Library
    drwx------   2 dr_unix  staff     24 Mar 21 20:30 Movies
    drwx------   2 dr_unix  staff     24 Mar 21 20:30 Music
    drwx------   6 dr_unix  staff    160 Mar 21 20:30 Pictures
    drwxr-xr-x   4 dr_unix  staff     92 Mar 22 18:06 Public
    drwxr-xr-x   4 dr_unix  staff     92 Aug 21  2001 Sites
    drwxr-xr-x   7 dr_unix  staff    194 Mar 23 11:21 personal
    -rw-r--r--   1 dr_unix  staff     15 Mar 18 10:47 test_1.txt
    -rw-r--r--   1 dr_unix  staff    243 Mar 18 10:47 who_list
    [localhost:~] dr_unix% 
    

Two of these files that deserve our attention for now are the '.' and '..' directories. '.' means 'this directory'.

    [localhost:~] dr_unix% ls .
    Adam.txt   Documents  Movies     Pictures   Sites      test_1.txt
    Desktop    Library    Music      Public     personal   who_list
    [localhost:~] dr_unix% ls
    Adam.txt   Documents  Movies     Pictures   Sites      test_1.txt
    Desktop    Library    Music      Public     personal   who_list
    [localhost:~] dr_unix% 
    

This may seem silly, but later we will see how handy this is. '..' represents 'the directory above this one':

    [localhost:~] dr_unix% pwd
    /Users/dr_unix
    [localhost:~] dr_unix% cd ..
    [localhost:/Users] dr_unix% pwd
    /Users
    [localhost:/Users] dr_unix% 
    

So we can see, even from these limited examples, that arguments and options give Unix commands a lot of power and flexibility, yet they do it in a very compact, some would say elegant, way.

The real power of ls, and a key feature of the Unix command line, lies in the wildcard feature, which we'll see in the next column.

You are encouraged to send Richard your comments, or to post them below.


Most Recent Mac OS X Command Line 101 Columns

Command Line History & Editing Your Commands
November 22nd

Pico: An Easy To Use Command Line Editor
November 1st

Understanding The "grep" Command In Mac OS X
October 4th

Command Line History & Editing Your Commands
September 6th

Mac OS X Command Line 101 Archives

Back to The Mac Observer For More Mac News!


Richard Burton is a longtime Unix programmer and a handsome brute. He spends his spare time yelling at the television during Colts and Pacers games, writing politically incorrect short stories, and trying to shoot the neighbor's cat (not really) nesting in his garage. He can be seen running roughshod over the TMO forums under the alias tbone1.



Today's Mac Headlines

[Podcast]Podcast - Apple Weekly Report #135: Apple Lawsuits, Banned iPhone Ad, Green MacBook Ad

We also offer Today's News On One Page!

Yesterday's News

 

[Podcast]Podcast - Mac Geek Gab #178: Batch Permission Changes, Encrypting Follow-up, Re-Enabling AirPort, and GigE speeds

We also offer Yesterday's News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Software
Browse the software section for over 17,000 Macintosh applications and software titles.

Hardware
Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .