Managing Files and Directories

 < Day Day Up > 

Basic Tiger Filesystem Navigation

The most basic commands for dealing with the Unix filesystem are those for moving around the filesystem (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.

Finding Your Current Location: 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.

 brezup:nermal Documents $ pwd /Users/nermal/Documents 

Table 10.1 shows the complete command documentation table for pwd.

Table 10.1. 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

Prints the physical path to the current working directory, with symbolic links resolved. This is the default under tcsh, but from bash, pwd appears to execute as pwd -L instead.


NOTE

Sometimes pwd doesn't respond with identical information to what you expect. For example, if you cd (change directory a command covered shortly) to the directory named /var/tmp, and issue the pwd -P command, you get /private/var/tmp as an answer. This is because Apple has put some of the normal Unix directories in weird places and pulled some trickery to get them to appear as though they're where they should be. This is a compromise forced on the system because it must be able to coexist with Mac OS on the same drive. If you want to see pwd return the same information as where you cded to, use pwd -L.


Listing Files in Various Locations: ls

The ls command lists files in the directory where you currently are. More properly, the ls command lists files anywhere in the filesystem, presuming that 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.

 brezup:nermal Documents $ cd / brezup:nermal / $ pwd / brezup: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. If you don't know where you are in the filesystem, pwd tells you. If you already know, there's no need to ask the computer to tell you.

NOTE

Many of the command listings shown here are based on a user account we've created for demo purposes, with the username nermal. You're welcome to use your own account instead of nermal for the examples.


If you want 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/.

 brezup:nermal / $ cd /Users/ brezup:nermal /Users $ ls Shared joray  miwa   nermal brezup:nermal /Users $ 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/:

 brezup:nermal / $ cd /Users/ brezup:nermal /Users $ ls Shared joray  miwa   nermal brezup:nermal /Users $ 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 want to list the files in the directory that is the parent of this directory (/Users/nermal/), you can use the relative path .. (the parent directory) to access it. To do this, use ls ../.

 brezup:nermal nermal $ cd /Users/nermal/Documents brezup:nermal Documents $ ls ../ Desktop                   Network Trash Folder      chown-output ...looks the same, still! 

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

 brezup:nermal nermal $ cd /Users/nermal/Documents brezup:nermal Documents $ 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 enable you to specify which files you want to list and what information you want to list about them. Table 10.2 shows the syntax for ls as well as some common and interesting options.

Table 10.2. Syntax and Selected Options for ls

ls

Lists directory contents.

ls [-ABCFGHLPRTWZabcdefghiklmnopqrstuwx1] [<file1> <file2> ...]

For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information.

If no operands are given, the contents of the current directory are displayed. If more than one operand is given, nondirectory operands are displayed first; directory and nondirectory operands are sorted separately and in lexicographical order.

-A

Lists all entries except for . and ... Always set for superuser.

-C

Forces multicolumn output. This is the default when output is to a terminal.

-F

Displays a slash (/) immediately after each pathname that is a directory, an asterisk (*) after each that is executable, an at sign (@) after each symbolic link, an equal sign (=) after each socket, a percent sign (%) after each whiteout, and a vertical bar (|) after each that is a FIFO.

-G

Enables colorized output. Equivalent to defining CLICOLOR in the environment.

-R

Recursively lists subdirectories encountered.

-T

When used with the -l option, displays complete time information for the file, including month, day, hour, minute, second, and year.

-a

Includes directory entries whose names begin with a dot (.).

-c

Uses time when file status was last changed for sorting or printing.

-d

Lists directories as plain files (not searched recursively).

-e

Prints the Access Control List associated with a file, if one exists.

-h

When used with the -l option, uses unit suffixes byte, kilobyte, megabyte, gigabyte, terabyte, and petabyte to reduce the number of digits to three or less using base 2 for sizes.

-l

Lists in long format. Long format lists the following: file mode, number of links, owner name, group name, number of bytes in the file, abbreviated month, day-of-month file was last modified, hour file last modified, minute file last modified, and the pathname.

-r

Reverses the order of the sort to get reverse lexicographical order or the oldest entries first.

-t

Sorts by time modified (most recently modified first) before sorting the operands by lexicographical order.

-u

Uses time of last access, instead of last modification of the file for sorting (-t) or printing (-l).

-x

The same as -C, except that the multicolumn output is produced with entries sorted across, rather than down, the columns.


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 group the file belongs to, and the size of files.

 brezup: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 ray   staff      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 ray   staff      264 Mar 23 14:59 TheFindByContentFolder drwxrwxrwx   4 ray   staff      264 Mar 23 14:46 TheVolumeSettingsFolder drwxrwxrwx   2 ray   staff      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 (not including directories) sizes contained in the directory. The total is in 512-byte blocks divide by 2 if you prefer your answer in kilobytes.

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 set of three values, r, w, and x, repeated three times. These three values 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. The user ray owns a few.

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 might be able to write to the file, that user 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 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 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 that 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, does 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 (or ls -al, the order of options doesn't typically matter in most commands).

 brezup: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 ray   staff      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 ray   staff      264 Mar 23 14:59 TheFindByContentFolder drwxrwxrwx   4 ray   staff      264 Mar 23 14:46 TheVolumeSettingsFolder drwxrwxrwx   2 ray   staff      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 drwxrwxrwt   1 root  admin       68 Jun 28 23:09 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 

Notice that several new files have appeared at the top of the listing relative to our previous output. These all start with . characters at the beginnings of their filenames. They aren't shown in the normal -l listing because files that start with . are understood by convention to be configuration files, and other types of content that the average user doesn't want to be troubled with seeing on a day-to-day basis when looking at the directory contents. You can make any file "slightly invisible" at the command line by adding a . to the beginning of its name.

Moving Around the Filesystem: cd, pushd, popd

Now that you know how to determine where you are in the filesystem 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 filesystem to whatever location you ask it. If you want 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:

 brezup:nermal var $ cd /var/log/ brezup:nermal log $ 

or:

 brezup:nermal var $ cd log/ brezup:nermal log $ 

TIP

Remember that you can always use ~ as a quick absolute path to your home directory. You can also use ~ to construct absolute paths to directories or files beneath your home directory. If you want to change into the directory named fizbin located in your home directory, you can use the cd command cd ~/fizbin.


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

 brezup:nermal log $ cd brezup:nermal nermal $ 

Table 10.3 show the command documentation table for cd.

Table 10.3. The Command Documentation Table for cd

cd

Changes working directory.

cd [-L|-P] [<dir>]

cd

<dir> is an absolute or relative pathname. The interpretation of the relative pathname depends on the CDPATH environment variable.

-L

Forces symbolic links to be followed.

-P

Uses physical directory structure instead of following symbolic links.

An argument of - is equivalent to $OLDPWD, which essentially takes you back to wherever it is that you last came from.


The bash and tcsh shells (similar to most others) also support a considerably more powerful way of navigating through the filesystem. This method, accessed through the pushd and popd commands, uses a computer structure called a stack. Using a stack enables you to go to another location and return to wherever you came from, without needing to remember the location and cd back.

NOTE

Computer scientists use the term stack as a friendly term for a data structure also known as a Last In, First Out (LIFO) structure. A classical LIFO structure has a single place where data can be put into or retrieved from the structure. If you put one piece of data into the structure, and then put in a second, you can't get the first back out again until you remove the second. See? The last thing you put in must be the first thing you take out. It's also called a stack because it works just like a stack of plates at a cafeteria. The last plate put on the stack (the one at the top of the stack) is the one that you take off first.


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 want to temporarily change to the directory /etc/httpd/, you could do so by issuing cd commands that you already know about, like so:

 brezup:nermal tmp $ pwd /private/var/tmp brezup:nermal tmp $ cd /etc/httpd brezup:nermal httpd $ pwd /private/etc/httpd ... (do some work here perhaps) ... brezup:nermal httpd $ cd /var/tmp brezup:nermal tmp $ pwd /private/var/tmp 

This works, but has the strong disadvantage that you need to use the wetware in your head to remember where you were and how to get back. It's also tedious if you're doing something that requires you to make this flip back and forth between the directories frequently. It seems like there should be a way to let the computer leave a trail of breadcrumbs for you, marking the waypoints of your travels about the directories as it were, and then to use this information to be able to automatically backtrack whenever needed. There is the solution is the pushd and popd commands.

As mentioned previously when discussing pwd, the results of looking at where you are might not agree entirely with where you think you've gone. This is the cause of the discrepancy seen between the locations cd'ed to, and the results of the pwd location checks shown here.

To do the same thing using pushd and popd is easier:

 brezup:nermal tmp $ pwd -L /var/tmp brezup:nermal tmp $ pushd /etc/httpd /etc/httpd /var/tmp brezup:nermal httpd $ pwd -L /etc/httpd brezup:nermal httpd $ popd /var/tmp brezup:nermal tmp $ 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:

 brezup:nermal tmp $ pushd /etc/httpd /etc/httpd /var/tmp brezup:nermal httpd $ pwd -L /etc/httpd brezup:nermal httpd $ pushd /Users /Users /etc/httpd /var/tmp brezup:nermal Users $ pwd -L /Users brezup:nermal Users $ popd /etc/httpd /var/tmp brezup:nermal httpd $ pwd -L /etc/httpd brezup:nermal httpd $ popd /var/tmp brezup:nermal tmp $ pwd -L /var/tmp 

To be able to switch back and forth between a pair of directories, simply 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 to where you came from, you just pushd. For example, you might do something like this:

 brezup:nermal tmp $ pushd /etc/httpd /etc/httpd /var/tmp brezup:nermal httpd $ pwd -L /etc/httpd brezup:nermal httpd $ pushd /var/tmp /etc/httpd brezup:nermal tmp $ pwd -L /var/tmp brezup:nermal tmp $ pushd /etc/httpd /var/tmp brezup:nermal httpd $ 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.

Tables 10.4 shows the complete command documentation table for pushd.

Table 10.4. The Command Documentation Table for pushd

pushd

Pushes a directory onto the directory stack.

pushd [-n] [dir]

pushd [-n] [+n] [-n]

Adds a directory to the top of the directory stack, or rotates the stack, making the new top of the stack the current working directory. With no arguments, exchanges the top two directories and returns 0, unless the directory stack is empty.

-n

Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated.

+n

Rotates the stack so that the nth directory (counting from the left of the list shown by dirs, starting with zero) is at the top.

-n

Rotates the stack so that the nth directory (counting from the right of the list shown by dirs, starting with zero) is at the top.

<dir>

Adds <dir> to the directory stack at the top, making it the new current working directory.


Table 10.5 shows the complete command documentation table for popd.

Table 10.5. The Command Documentation Table for popd

popd

Removes entries from the directory stack.

popd [-n] [+n] [-n]

With no arguments, removes the top directory from the stack, and performs a cd to the new top directory.

-n

Suppresses the normal change of directory when removing directories from the stack so that only the stack is manipulated.

+n

Removes the nth entry counting from the left of the list shown by dirs, starting with zero. For example: popd +0 removes the first directory; popd +1, the second.

-n

Removes the nth entry counting from the right of the list shown by dirs, starting with zero. For example: popd -0 removes the last directory; popd -1, the next to last.


     < Day Day Up > 


    Mac OS X Tiger Unleashed
    Mac OS X Tiger Unleashed
    ISBN: 0672327465
    EAN: 2147483647
    Year: 2005
    Pages: 251

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net