< Day Day Up > |
Basic Tiger Filesystem NavigationThe 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: pwdThe 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.
Listing Files in Various Locations: lsThe 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.
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.
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, popdNow 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 $
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.
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.
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.5 shows the complete command documentation table for popd.
|
< Day Day Up > |