3. Your UNIX Account
Contents:
Once you log in, you can use the many facilities UNIX provides. As an authorized system user, you have an account that gives you:
3.1 The UNIX FilesystemA file is the unit of storage in UNIX, as in many other systems. A file can hold anything: text (a report you're writing, a to-do list), a program, digitally encoded pictures or sound, and so on. All of those are just sequences of raw data until they are interpreted by the right program. In UNIX, files are organized into directories. A directory is actually a special kind of file where the system stores information about other files. A directory can be thought of as a place, so that files are said to be contained in directories and you are said to work inside a directory. (If you've used a Macintosh or Microsoft Windows computer, a UNIX directory is a lot like a folder. MS-DOS and UNIX directories are very similar.) 3.1.1 Your Home DirectoryWhen you log in to UNIX, you're placed in a directory called your home directory . This home directory, a unique place in the UNIX filesystem, contains the files you use almost every time you log in. In your home directory, you can make your own files. As you'll see in a minute, you can also store your own directories within your home directory. Like folders in a file cabinet, this is a good way to organize your files. 3.1.2 Your Working DirectoryYour working directory (sometimes called your current working directory) is the directory you're currently working in. At the start of every session, your home directory is your working directory. You may change to another directory, in which case the directory you move to becomes your working directory. Unless you tell UNIX otherwise, all commands that you enter apply to the files in your working directory. In the same way, when you create files, they're created in your working directory. 3.1.3 The Directory Tree
All directories on a UNIX system are organized into a hierarchical structure
that you can imagine as a family tree.
The parent directory of the tree is known as the
root directory
and is written as a forward slash ( The root contains several directories. Figure 3.1 shows the top of an imaginary UNIX filesystem tree - the root directory and some of the directories under the root. Figure 3.1: Example of a directory treebin , etc , users , tmp , and usr are some of the subdirectories (child directories) of root . These are fairly standard directories and usually contain specific kinds of system files. For instance, bin contains many UNIX commands. Not all systems have a directory named users ; it may be called u , home , and/or be located in some other part of the filesystem. In our example, the parent directory of users (one level above) is root . It also has two subdirectories (one level below), john and carol . On a UNIX system, each directory has one parent directory and may have one or more subdirectories. [1] A subdirectory (like carol ) can have its own subdirectories (like work and play ), to a limitless depth for practical purposes.
To specify a file or directory location, you write its pathname . A pathname is like the address of the directory or file in the UNIX filesystem. We'll look at pathnames in a moment. On a basic UNIX system, all files in the filesystem are stored on disks connected to your computer. It isn't always easy to use the files on someone else's computer or for someone on another computer to use your files. Your system may have an easier way: a networked filesystem (with a name like NFS or RFS). Networked filesystems make a remote computer's files appear as if they're part of your computer's directory tree. For instance, your computer in Los Angeles might have a directory named boston . When you look in that subdirectory, you'll see some (or all) of the directory tree from your company's computer in Boston. Your system administrator can tell you if your computer has any networked filesystems. 3.1.4 Absolute Pathnames
As you saw above,
the UNIX filesystem organizes its files and directories in an inverted tree
structure with the root directory at the top.
An
absolute pathname
tells you the path of directories you must
travel to get
from the root to the directory or file you want.
In a pathname, put slashes ( For example, /users/john is an absolute pathname. It locates just one directory. Here's how:
Be sure not to type spaces anywhere in the pathname. Figure 3.2 shows this structure. Figure 3.2: Absolute path of directory johnIf you look at Figure 3.2 , you'll see that the directory john has a subdirectory named work . Its absolute pathname is /users/john/work . The root is always indicated by the slash (/) at the start of the pathname. 3.1.5 Relative PathnamesYou can also locate a file or directory with a relative pathname . A relative pathname gives the location relative to your working directory. Unless you use an absolute pathname (starting with a slash), UNIX assumes that you're using a relative pathname. Like absolute pathnames, relative pathnames can go through more than one directory level by naming the directories along the path. For example, if you're currently in the users directory (see Figure 3.2 ), the relative pathname to the carol directory below is simply carol . The relative pathname to the play directory below that is carol/play . Notice that neither of the pathnames in the previous paragraph starts with a slash. That's what makes them relative pathnames! These pathnames start at the working directory, not the root directory. 3.1.5.1 ExerciseHere's a short but important exercise. The example above explained the relative pathname carol/play . What do you think UNIX would say about the pathname /carol/play ? (Look again at Figure 3.2 .) UNIX would say "No such file or directory." Why? (Please think about that before you read more. It's very important and it's one of the most common beginner's mistakes.) Here's the answer. Because it starts with a slash, the pathname /carol/play is an absolute pathname that starts from the root. It says to look in the root directory for a subdirectory named carol . But there is no subdirectory named carol one level directly below the root, so the pathname is wrong. The only absolute pathname to the play directory is /users/carol/play . 3.1.5.2 Relative pathnames upYou can go up the tree by using the shorthand ".." (dot dot) for the parent directory. As you saw above, you can also go down the tree by using subdirectory names. In either case (up or down), separate each level by a slash (/). Figure 3.3 shows a part of Figure 3.1 . If your working directory in the figure is work , there are two pathnames for the play subdirectory of carol . You already know how to write the absolute pathname, /users/carol/play . You can also go up one level (with "..") to carol , then go down the tree to play . Figure 3.3 shows that. Figure 3.3: Relative pathname from work to playThe relative pathname would be ../play . It would be wrong to give the relative address as carol/play . Using carol/play would say that carol is a subdirectory of your working directory instead of what it is in this case: the parent directory. Absolute and relative pathnames are totally interchangeable. UNIX commands simply follow whatever path you specify to wherever it leads. If you use an absolute pathname, the path starts from the root. If you use a relative pathname, the path starts from your working directory. Choose whichever is easier at the moment. 3.1.6 Changing Your Working DirectoryWhen you know the absolute or relative pathname of a directory, you can move up and down the UNIX directory tree. 3.1.6.1 pwdTo find which directory you're currently in, use the pwd (print working directory) command. The pwd command takes no arguments.
% pwd prints the absolute pathname of your working directory. 3.1.6.2 cdYou can change your working directory to any directory (including another user's directory - if you have permission) with the cd (change directory) command. The cd command has the form:
The argument is an absolute or a relative pathname (whichever is easier) for the directory you want to change to.
% Here's a handy tip: the command cd , with no arguments, takes you to your home directory from wherever you are in the filesystem. Note that you can only change to another directory. You cannot cd to a filename. If you try, UNIX will give you an error message:
% /etc/passwd is a file that contains information about users allowed to log in to the system. 3.1.7 Files in the Directory TreeA directory can hold subdirectories. And, of course, a directory can hold files. Figure 3.4 is a close-up of the filesystem around john 's home directory. The four files are shown along with the work subdirectory. Figure 3.4: Files in the directory treePathnames to files are made the same way as pathnames to directories. For example, if your working directory is users , the relative pathname to the work directory below would be john/work . The relative pathname to the ch1 file would be john/ch1 . 3.1.8 Listing FilesTo use the cd command, you must decide which entries in a directory are subdirectories and which are files. The ls command lists the entries in the directory tree. 3.1.8.1 lsWhen you enter the ls command, you'll get a listing of the files and subdirectories contained in your working directory. The syntax is:
If you've just logged in for the first time, entering ls without any arguments may seem to do nothing. This isn't surprising because you haven't made any files in your working directory. If you have no files, nothing is displayed; you'll simply get a new shell prompt.
% But if you've already made some files or directories in your account, those names are displayed. The output depends on what's in your directory. The display should look something like this:
% (Some systems display filenames in a single column. If yours does, you can change the display to columns with the -x option.) ls has a lot of options that change the information and display format. The -a option (for all ) is guaranteed to show you some more files, as in the following example:
% You'll always see at least two new entries with the names "." (dot) and ".." (dot dot). As mentioned earlier, .. is always the relative pathname to the parent directory, and a single . always stands for any working directory. There may also be other files, like .profile or .exrc . Any entry whose name begins with a dot is hidden - it will be listed only if you use ls -a . To get more information about each file, add the -l option. (That's a lowercase letter "L" for long .) This option can be used alone, or in combination with -a , as shown in Figure 3.5 . Figure 3.5: Output from ls -alThe long format provides the following information about each file:
Notice especially the columns that list the owner and group of the files, and the access modes (also called permissions). The person who creates a file is its owner; if you've created any files (or the system administrator did it for you), this column should show your username. You also belong to a group, to which you were assigned by the system administrator. Files you create will either be marked with the name of your group or, in some cases, the group that owns the directory. The permissions control who can read, write (modify), or execute the file (if it's a program). The permissions have ten characters. The first character shows the file type (directory or plain file). The second through the fourth characters show the permissions for the file's owner - yourself if you created the file. The fifth through the seventh characters show permissions for other members of the file's group. The eighth through the tenth characters show permissions for all other users.
For example, the permissions for
.profile
are
In the case of directories, You can use the chmod command to change the permissions of your files and directories. See the section of this chapter called "Protecting and Sharing Files." If you need to know only which files are directories and which are executable files, you can use the -F option. If you give the pathname to a directory, ls will list the directory but it will not change your working directory. The pwd command in the following example shows that:
%
ls -F
puts a / (slash) at the end of each directory name.
(The directory name doesn't really have a slash in it; that's just the
shortcut
ls -F
uses with a directory.) In our example,
guide
and
ideas
are directories. You can verify this by
using
ls -l
and noting the "d" in the first field of the
output. Files with an execute status ( On Linux and other systems with the GNU version of ls , you may be able to see names in color. For instance, directories could be green and program files could be yellow. Like almost everything on UNIX, of course, this is configurable - and the details are more than we can cover in an introductory book. Try typing ls --color and see what happens. (It's time for our familiar mantra: "check your documentation"; see Chapter 7, Where to Go from Here .) 3.1.8.2 Exercise: Exploring the filesystemYou're now equipped to explore the filesystem with cd , ls , and pwd . Take a tour of the directory system, hopping one or many steps at a time, with a mixture of cd and pwd commands.
| ||||||||||||||||||||||
|