Mac OS X Unleashed

Mac OS X Unleashed

By John Ray and William C. Ray

Basic File System Navigation

The most basic commands for dealing with the Unix file system are those for moving around the file system (changing that "somewhere" that the shell always is), and for listing the contents of directories (finding out what's in the same location as the shell or in some other directory). Before you start moving around, however, it's a good idea to be able to find out where you are.

Where Are You? pwd

The pwd command (print working directory) prints the full path to the current working directory—the location where you "are" at the moment in this particular shell.

[localhost:~] nermal% pwd

     /Users/nermal

The command documentation table for pwd is as shown in Table 12.4.

Table 12.4. The Command Documentation Table for pwd

pwd Prints current working directory
pwd [-L|P]  
-L Prints the logical path to the current working directory, as defined by the shell in the environment variable PWD.
-P Default. Prints the physical path to the current working directory, with symbolic links resolved.

Listing Files: ls

The ls command lists files in the directory where you currently are. More properly, the ls command will list files anywhere in the file system, presuming you have permissions with which to do so. If you don't specify any other directory for it to list, ls defaults to listing the files in the current working directory.

For example, to list the files in the current working directory, simply type ls.

[localhost:~] nermal% cd /
[localhost:/] nermal% pwd

     /
[localhost:/] nermal% ls
     AppleShare PDS            Network                   etc
     Applications              System                    mach
     Applications (Mac OS 9)   System Folder             mach.sym
     Cleanup At Startup        TheFindByContentFolder    mach_kernel
     Desktop DB                TheVolumeSettingsFolder   private
     Desktop DF                Trash                     sbin
     Desktop Folder            Users                     tmp
     Developer                 Volumes                   usr
     Documents                 bin                       var
     Late Breaking News        cores                     vol.tar
     Library                   dev                       ???T+???Blank1

This example shows that the directory / contains 33 things. From this listing, you can't tell which of those things are directories and which of those things are files.

There's really no need to issue the pwd command as shown in the preceding example. It's been included here for the sake of clarity. If you already know where you are in the file system, there's no need to ask the computer to tell you.

If you would like to list the files in a directory other than the one that you are currently in, simply specify the path to the directory after the ls command. The path to specify can be either a relative or an absolute path. For example, to list the files in a directory named /Users/nermal/ if you're in the directory /Users/, simply type ls nermal/.

[localhost:/] nermal% cd /Users/
[localhost:/Users] nermal% ls
     Shared joray  miwa   nermal

[localhost:/Users] nermal% ls nermal/

     Desktop                   Network Trash Folder      chown-output
     Documents                 Pictures                  myfile
     Library                   Public                    output-sample6
     Movies                    Sites                     su-output
     Music                     TheVolumeSettingsFolder   typescripts

You could also produce this same output by using the absolute path. Instead of the relative path nermal/ from the current directory /Users/, you could look explicitly in /Users/nermal/:

[localhost:/] nermal% cd /Users/
[localhost:/Users] nermal% ls
     Shared joray  miwa   nermal

[localhost:/Users] nermal% ls /Users/nermal/

     Desktop                   Network Trash Folder      chown-output
     Documents                 Pictures                  myfile
     Library                   Public                    output-sample6
     Movies                    Sites                     su-output
     Music                     TheVolumeSettingsFolder   typescripts

Or, if you're in the directory /Users/nermal/Documents/ and would like to list the files in the directory that is the parent of this directory (/Users/nermal/), you can type ls ../.

[localhost:/Users] nermal% cd /Users/nermal/Documents
[localhost:~/Documents] nermal% ls ../

     Desktop                   Network Trash Folder      chown-output
     Documents                 Pictures                  myfile
     Library                   Public                    output-sample6
     Movies                    Sites                     su-output
     Music                     TheVolumeSettingsFolder   typescripts

Likewise, if you want to list the contents of /Users/nermal/typescripts/, and you're in /Users/nermal/Documents/, you could type

[localhost:/Users] nermal% cd /Users/nermal/Documents
[localhost:~/Documents] nermal% ls ../typescripts/

     typescript          typescript-copy-2   typescript2         typescript5
     typescript-copy     typescript-copy-3   typescript4

Like most Unix commands, the ls command has a plethora of options from which to choose. These options allow you to specify which files you want to list and what information you want to list about them. Table 12.5 shows the available options and outputs for ls.

Table 12.5. The Command Documentation Table for ls

ls Lists files or directory contents.
ls [-ACFLRSTWadfgilnoqrsktcux1] < file1 > < file2 > ...
ls [-ACFLRSTWadfgilnoqrsktcux1]
-A Lists all entries except for "." and "..". Always set for super user.
-C Forces multi-column output. This is the Default when output is to a terminal.
-F Displays a symbol, if applicable, after each file to denote the following: slash (/) for a directory; asterisk (*) for an executable; an at sign (@) for a symbolic link; a percent sign (%) for a whiteout; an equal sign (=) for a socket; a vertical bar (|) for a FIFO.
-L If the argument is a symbolic link, the file or directory the link references rather than the link itself is displayed.
-R Recursively lists subdirectories.
-S Sorts by size, largest file first.
-T Displays complete time information, including month, day, hour, minute, second, and year.
-W Displays whiteouts.
-a Lists all files in the directory, including files whose names begin with a dot (.).
-d If the argument is a directory, it is listed as a plain file, rather than listing its contents. If the argument is a symbolic link, its link information is not displayed.
-f Does not sort output.
-g Does nothing. Is kept for compatibility with older versions of ls.
-i Lists the argument's serial number (inode number) .
-l Lists in long format. Displays file mode, number of links, owner name, group name, size of the file in bytes, date and time file was last modified, and the file. If displayed to a terminal, the first line of output is the total number of 512-byte blocks used by the files in the directory.
-n Displays user and group ID as numbers rather than names in a long (-l) output.
-o Includes file flags in a long (-l) output.
-q Forces printing of non-graphic characters in filenames as character ?. This is the default when output is to a terminal.
-r Reverses sort order to reverse alphabetic order; smallest first or oldest first, as appropriate.
-s Displays file size in 512-byte blocks, where partial units are rounded up to the next integer value. If the output is to a terminal, first line displayed is the total number of 512-byte blocks used by files in the directory.
-k Modifies the -s option to report sizes in kilobytes.
-t Sorts by time modified (most recently modified first) before sorting in alphabetic order.
-c Uses time when file status was last changed for sorting (-t) or printing (-l).
-u Uses time of last access for sorting (-t) or printing (-l)
-x Forces multi-column output sorted across the page rather than down the page.
-v Forces unedited printing of non-graphic characters. This is the default when output is not to a terminal.
-1 Forces output to one entry per line. This is the default when output is not to a terminal.
-1, -C, -l, and -x options override each other. The last one specified determines the format used.
-c and -u options override each other. The last one specified determines the file time used.

You can use an ls command like this to produce a listing that shows the contents of the root directory and indicates the following for each file (or directory): who the owner of the file is, what the group that the file belongs to is, as well as the size of files.

 [localhost:/] nermal% ls -l

     total 13232
     -rwxrwxrwx   1 root  wheel   106496 Apr 20 14:59 AppleShare PDS
     drwxrwxrwx  25 root  admin      806 Apr 18 11:05 Applications
     drwxrwxrwx  18 root  wheel      568 Apr 20 14:54 Applications (Mac OS 9)
     drwxrwxrwx   2 root  wheel      264 Apr  6 12:24 Cleanup At Startup
     -rwxrwxrwx   1 root  wheel   212992 Apr 20 14:59 Desktop DB
     -rwxrwxrwx   1 root  wheel  1432466 Apr 20 14:57 Desktop DF
     drwxrwxrwx   6 root  staff      264 Apr  4 11:51 Desktop Folder
     drwxrwxr-x  12 root  admin      364 Mar  1 20:29 Developer
     drwxrwxrwx   6 root  wheel      264 Apr  4 14:20 Documents
     -rwxrwxrwx   1 root  wheel        0 Apr  4 14:11 Late Breaking News
     drwxrwxr-x  21 root  admin      670 Apr 18 11:04 Library
     drwxr-xr-x   6 root  wheel      264 Apr  4 12:47 Network
     drwxr-xr-x   3 root  wheel       58 Apr 12 00:51 System
     drwxrwxrwx  40 root  wheel     1316 Apr 20 14:50 System Folder
     drwxrwxrwx   2 root  wheel      264 Mar 23 14:59 TheFindByContentFolder
     drwxrwxrwx   4 root  wheel      264 Mar 23 14:46 TheVolumeSettingsFolder
     drwxrwxrwx   2 root  wheel      264 Apr 20 14:58 Trash
     drwxr-xr-x   6 root  wheel      160 Apr 16 12:37 Users
     drwxrwxrwt   6 root  wheel      264 Apr 20 15:00 Volumes
     drwxr-xr-x  33 root  wheel     1078 Apr 16 09:40 bin
     lrwxrwxr-t   1 root  admin       13 Apr 20 15:00 cores -> private/cores
     dr-xr-xr-x   2 root  wheel      512 Apr 20 15:00 dev
     lrwxrwxr-t   1 root  admin       11 Apr 20 15:00 etc -> private/etc
     lrwxrwxr-t   1 root  admin        9 Apr 20 15:00 mach -> /mach.sym
     -r--r--r--   1 root  admin   652352 Apr 20 15:00 mach.sym
     -rw-r--r--   1 root  wheel  4039744 Mar 30 23:46 mach_kernel
     drwxr-xr-x   7 root  wheel      264 Apr 20 15:00 private
     drwxr-xr-x  56 root  wheel     1860 Apr 16 09:41 sbin
     lrwxrwxr-t   1 root  admin       11 Apr 20 15:00 tmp -> private/tmp
     drwxr-xr-x  10 root  wheel      296 Apr 12 14:45 usr
     lrwxrwxr-t   1 root  admin       11 Apr 20 15:00 var -> private/var
     -rw-r--r--   1 root  admin    10240 Apr 16 09:35 vol.tar
     -rwxrwxrwx   1 root  wheel   221696 Apr  4 13:57 ???T+???Blank1

The output might look a little confusing at first, but it breaks down into parts that are easy to understand.

The first line contains information telling you the total sum for all the file sizes contained in the directory. The total is in 512-byte blocks.

Next come lines detailing the contents of the directory, one file or directory listed per line.

At the beginning of each line are 10 characters. These indicate the values of 10 flags that belong to the file. The first flag indicates whether the file is a directory, a symbolic link (Unix for alias), or just a plain normal file. If the first flag is a d, the indicated item is a directory. If it is an l, the item is a link. If it is only a -, the item is a file. Next is a repeating a set of three values r, w, and x repeated three times. These specify the read flag, the write flag, and the execute flag for each user who owns the file, the group that owns the file, and all other users on the system. If a - is shown instead of an r, w, or x, the user, the group, or everybody on the system is not allowed to perform whatever action—read, write, or execute—that the flag is missing for.

Shortly following the 10 flag characters, each line contains an entry indicating the user who owns the file; in this case, root owns many of the files shown.

Following the information indicating the owner of the file is another entry indicating the group that owns the file. Group ownership of a file is not as stringent as the user ownership of a file. The individual owner of a file is the only user allowed to modify the permissions of a file. So, although a user who belongs to the group that owns the file may be able to write to the file, he or she cannot modify the flags indicating what the permissions are for the file.

Next is an entry indicating the size of the file in bytes. Entries for files indicate the full size of the file on disk. Entries for directories indicate another value that is loosely associated with the number of entries that the directory contains.

Following the size of the file comes an entry indicating the date of the most recent modification of the file. If the file was modified within the last year, the date and time are given; otherwise, the month, day, and year are given.

Finally, each entry lists the filename. Note that the filenames are identical to the name shown by the use of ls from our very first example, with the exception of core, etc, tmp, mach, and var. Each of these entries is followed by an odd arrow that points to a path. Note the file type for these is indicated by ls as a symbolic link. Just as a Mac OS alias points to a file or directory in another location, a symbolic link also points to a file or directory in another location. The information shown following the arrow is the path to which each particular entry points.

To prevent clutter, the ls command, by default, will not show certain files and directories that are expected to be configuration files or to contain maintenance or control information. Specifically, files or directories whose names begin with a dot (.) are not shown. Still, if you want to see them, there is an ls option that will allow this. If you want to see absolutely everything in the directory, add the -a option to the ls command; for example, ls -la.

[localhost:/] nermal% ls -al

     total 13264
     drwxrwxr-t  39 root  admin     1282 Apr 20 15:00 .
     drwxrwxr-t  39 root  admin     1282 Apr 20 15:00 ..
     -rwxrwxrwx   1 root  admin     8208 Apr 18 11:05 .DS_Store
     d-wx-wx-wx   2 root  admin      264 Apr  4 12:20 .Trashes
     -r--r--r--   1 root  wheel      142 Feb 25 03:05 .hidden
     dr--r--r--   2 root  wheel      224 Apr 20 15:00 .vol
     -rwxrwxrwx   1 root  wheel   106496 Apr 20 14:59 AppleShare PDS
     drwxrwxrwx  25 root  admin      806 Apr 18 11:05 Applications
     drwxrwxrwx  18 root  wheel      568 Apr 20 14:54 Applications (Mac OS 9)
     drwxrwxrwx   2 root  wheel      264 Apr  6 12:24 Cleanup At Startup
     -rwxrwxrwx   1 root  wheel   212992 Apr 20 14:59 Desktop DB
     -rwxrwxrwx   1 root  wheel  1432466 Apr 20 14:57 Desktop DF
     drwxrwxrwx   6 root  staff      264 Apr  4 11:51 Desktop Folder
     drwxrwxr-x  12 root  admin      364 Mar  1 20:29 Developer
     drwxrwxrwx   6 root  wheel      264 Apr  4 14:20 Documents
     -rwxrwxrwx   1 root  wheel        0 Apr  4 14:11 Late Breaking News
     drwxrwxr-x  21 root  admin      670 Apr 18 11:04 Library
     drwxr-xr-x   6 root  wheel      264 Apr  4 12:47 Network
     drwxr-xr-x   3 root  wheel       58 Apr 12 00:51 System
     drwxrwxrwx  40 root  wheel     1316 Apr 20 14:50 System Folder
     drwxrwxrwx   2 root  wheel      264 Mar 23 14:59 TheFindByContentFolder
     drwxrwxrwx   4 root  wheel      264 Mar 23 14:46 TheVolumeSettingsFolder
     drwxrwxrwx   2 root  wheel      264 Apr 20 14:58 Trash
     drwxr-xr-x   6 root  wheel      160 Apr 16 12:37 Users
     drwxrwxrwt   6 root  wheel      264 Apr 20 15:00 Volumes
     drwxr-xr-x  33 root  wheel     1078 Apr 16 09:40 bin
     lrwxrwxr-t   1 root  admin       13 Apr 20 15:00 cores -> private/cores
     dr-xr-xr-x   2 root  wheel      512 Apr 20 15:00 dev
     lrwxrwxr-t   1 root  admin       11 Apr 20 15:00 etc -> private/etc
     lrwxrwxr-t   1 root  admin        9 Apr 20 15:00 mach -> /mach.sym
     -r--r--r--   1 root  admin   652352 Apr 20 15:00 mach.sym
     -rw-r--r--   1 root  wheel  4039744 Mar 30 23:46 mach_kernel
     drwxr-xr-x   7 root  wheel      264 Apr 20 15:00 private
     drwxr-xr-x  56 root  wheel     1860 Apr 16 09:41 sbin
     lrwxrwxr-t   1 root  admin       11 Apr 20 15:00 tmp -> private/tmp
     drwxr-xr-x  10 root  wheel      296 Apr 12 14:45 usr
     lrwxrwxr-t   1 root  admin       11 Apr 20 15:00 var -> private/var
     -rw-r--r--   1 root  admin    10240 Apr 16 09:35 vol.tar
     -rwxrwxrwx   1 root  wheel   221696 Apr  4 13:57 ???T+???Blank1

Moving Around the File System: cd, pushd, popd

Now that you know how to determine where you are in the file system and how to list the files in a particular location, it's time to learn how to change your location. Unix provides two primary mechanisms by which you can do this. The first of these is the cd (change directory) command. This command does exactly what you would expect from its name: It changes your location in the file system to whatever location you ask it. If you would like to change the current working directory from /var to /var/log/, you can type cd /var/log/. Because cd, like most Unix commands, accepts either relative or absolute paths, you can also make this change by typing cd log/, as shown here:

[localhost:/var] nermal% cd /var/log

[localhost:/var/log] nermal%

[localhost:/var] nermal% cd log

[localhost:/var/log] nermal%

The cd command can also be used without an argument, in which case it will assume that you want to go to your home directory, and will take you to that location:

[localhost:/var/log] nermal% cd

[localhost:~] nermal%

The command documentation table for cd is shown in Table 12.6.

Table 12.6. The Command Documentation Table for cd

cd Changes working directory
cd <directory>  
cd  
<directory> is an absolute or relative pathname. The interpretation of the relative pathname depends on the CDPATH environment variable.
The following environment variables affect the execution of cd:
HOME If cd is invoked without any arguments and the $HOME exists, $HOME becomes the new working directory.
CDPATH If <directory> does not begin with /, ., or .., cd searches for the directory relative to each directory named in CDPATH variable, in the order listed. If the new working directory is derived from $CDPATH, it is printed to standard output.

The tcsh shell (similar to most others) also supports a considerably more powerful way of navigating through the file system. This method, accessed through the pushd and popd commands, makes use of a computer structure called a stack. Using a stack allows you to go to another location, and return to wherever you came from, without needing to remember the location and cd back.

The pushd and popd commands work in concert. pushd puts the current directory on the stack, and takes you to whatever directory you tell it to. popd takes you to whatever directory is on top of the stack, and removes that directory from the stack.

For example, if you're in /var/tmp/ and you'd like to temporarily change to the directory /etc/httpd/, you could do so by issuing the following set of commands:

[localhost:/var/tmp] nermal% cd /var/tmp
[localhost:/var/tmp] nermal% pwd

     /private/var/tmp

[localhost:/var/tmp] nermal% cd /etc/httpd
[localhost:/etc/httpd] nermal% pwd

     /private/etc/httpd

[localhost:/etc/httpd] nermal% cd /var/tmp
[localhost:/var/tmp] nermal% pwd

     /private/var/tmp

But moving around like this is relatively inefficient, and if you do much in directory /etc/httpd/ before deciding to return, you might forget where it was that you'd come from. Instead, you might want to use the pushd and popd commands. To do the same thing using them, you could type

[localhost:/var/tmp] nermal% cd /var/tmp
[localhost:/var/tmp] nermal% pwd -L

     /var/tmp

[localhost:/var/tmp] nermal% pushd /etc/httpd

     /etc/httpd /var/tmp

[localhost:/etc/httpd] nermal% pwd -L

     /etc/httpd

[localhost:/etc/httpd] nermal% popd

     /var/tmp

[localhost:/var/tmp] nermal% pwd -L

     /var/tmp

Because the stack of directories is arbitrarily deep, you can push multiple items on before you start popping them off. For example:

[localhost:/var/tmp] nermal% cd /var/tmp
[localhost:/var/tmp] nermal% pushd /etc/httpd

/etc/httpd /var/tmp

[localhost:/etc/httpd] nermal% pwd -L

     /etc/httpd

[localhost:/etc/httpd] nermal% pushd /Users

     /Users /etc/httpd /var/tmp

[localhost:/Users] nermal% pwd -L

     /Users

[localhost:/Users] nermal% popd

     /etc/httpd /var/tmp

[localhost:/etc/httpd] nermal% pwd -L

     /etc/httpd

[localhost:/etc/httpd] nermal% popd

     /var/tmp

[localhost:/var/tmp] nermal% pwd -L

     /var/tmp

To be able to switch back and forth between a pair of directories, don't give an argument to pushd. It will pop the directory on top of the stack, push the current directory on, and switch you to the directory that it popped off. If that's a little confusing, just remember that if you've just come from somewhere using pushd, you can get back again using pushd with no arguments. When you're back, you've again just come from somewhere using pushd, so to get back you just…. For example, you might do something like this:

[localhost:/var/tmp] nermal% cd /var/tmp
[localhost:/var/tmp] nermal% pushd /etc/httpd

     /etc/httpd /var/tmp

[localhost:/etc/httpd] nermal% pwd -L

     /etc/httpd

[localhost:/etc/httpd] nermal% pushd

     /var/tmp /etc/httpd
[localhost:/var/tmp] nermal% pwd -L

     /var/tmp
[localhost:/var/tmp] nermal% pushd

     /etc/httpd /var/tmp

[localhost:/etc/httpd] nermal% pwd -L

     /etc/httpd

( And this can go on forever)

Finally, note that the stack used by tcsh isn't technically a classical LIFO structure because it has a side door. You might have noticed that pushd prints out the stack of directories after it's used each time. If you need to switch to a directory that's not at the top, issue pushd + n , where n is the depth of the directory you want to go to. Doing so shuffles that directory out to the top of the stack, and switches you to it.

The command documentation table for pushd is shown in Table 12.7.

Table 12.7. The Command Documentation Table for pushd

pushd Pushes a directory on to the directory stack.
pushd [-p] [-l] [-n | -v] [ <dir> | + <n> ]
pushd  
Without arguments, pushd exchanges the top two elements of the directory stack. If pushdt o home is set, pushd without arguments does pushd ~ , like cd.
-p Overrides the pushdsilent shell variable. (The pushds i lent shell variable can be set to prevent pushd from printing the final directory stack.)
-l Lists the output in long form.
-v Prints one entry per line, preceded by their stack positions.
-n Wraps entries before they reach the edge of the screen.
<dir> Pushes the current directory into the stack and changes to the specified <dir> .
+ <n> Rotates the <n> th directory to the top of the stack and changes to that directory.
If both -n and -v are specified, -v takes precedence.

The command documentation table for popd is shown in Table 12.8.

Table 12.8. The Command Documentation Table for popd

popd Pops the directory stack and changes to the new top directory.
popd [-p] [-l] [-n | -v] [+ <n> ]
popd  
Without arguments, popd pops the directory stack and returns to the new top directory. Elements in the directory stack are numbered from 0 starting at the top.
-p Overrides the pushdsilent shell variable. (The pushdsilent shell variable can be set to prevent popd from printing the final directory stack.)
-l Lists the output in long form.
-v Prints one entry per line, preceded by their stack positions.
-n Wraps entries before they reach the edge of the screen.
+ <n> Discards the <n> th directory in the stack.
If both -n and -v are specified, -v takes precedence.

Share ThisShare This

Informit Network