start page | rating of books | rating of authors | reviews | copyrights

Book HomeLearning the Unix Operating SystemSearch this book

Chapter 3. Using Your Unix Account

Contents:

The Unix Filesystem
Looking Inside Files with less
Protecting and Sharing Files
Graphical Filesystem Browsers
Completing File and Directory Names
Changing Your Password
Customizing Your Account

Once you log in, you can use the many facilities that Unix provides. As an authorized system user, you have an account that gives you:

3.1. The Unix Filesystem

A file is the unit of storage in Unix, as in most 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're 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. You can think of a directory 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 similar to a folder.)

This section introduces the Unix filesystem. Later sections in this chapter show how you can look in files and protect them. Chapter 4 has more information.

3.1.1. Your Home Directory

When you log in to Unix, you're placed in a directory called your home directory. This 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 Directory

Your working directory (also called your current directory) is the directory you're currently working in. Every time you log in, 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 unless you specify another directory. For instance, if you type the command pico report, the Pico editor is started on a file named report in your working directory. But if you type a command such as pico /home/joan/report, a report file is edited in a different directory--without changing your working directory. You'll learn more about this when we cover pathnames later in this chapter.

If you have more than one terminal window open, or you're logged in on several terminals at the same time, each session has its own working directory. Changing the working directory in one session doesn't affect others.

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 (the directory that contains all other directories) is known as the root directory and is written as a forward slash (/).

The root contains several directories. Figure 3-1 shows a visual representation of the top of a Unix filesystem tree: the root directory and some directories under the root.

Figure 3-1

Figure 3-1. Example of a directory tree

bin, etc, users, tmp, and usr are some of the subdirectories (child directories) of the root directory. These subdirectories are fairly standard directories; they usually contain specific kinds of system files. For instance, bin contains many Unix programs. Not all systems have a directory named users. It may be called u or home, and/or it may be located in some other part of the filesystem.

In our example, the parent directory of users (one level above) is the root directory. It has two subdirectories (one level below), john and carol. On a Unix system, each directory has only one parent directory, but it may have one or more subdirectories.[7] A subdirectory (such as carol) can have its own subdirectories (such as work and play), up to a limitless depth for practical purposes.

[7] On most Unix systems, the root directory, at the top of the tree, is its own parent. Some systems have another directory above the root.

To specify a file or directory location, write its pathname. A pathname is like the address of the directory or file in the Unix filesystem. We 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. Networked filesystems make a remote computer's files appear as if they're part of your computer's directory tree. For instance, a computer in Los Angeles might have a directory named boston with some of the directory tree from a company's computer in Boston. Or individual users' home directories may come from various computers, but all be available on your computer as if they were local files. The system staff can help you understand and configure your computer's filesystems to make your work easier.

3.1.4. Absolute Pathnames

As you saw earlier, 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 (/) between the directory names.

For example, /users/john is an absolute pathname. It locates one (only one!) directory. Here's how:

  • The root is the first "/"

  • The directory users (a subdirectory of root)

  • The directory john (a subdirectory of users)

Be sure that you do not type spaces anywhere in the pathname. Figure 3-2 shows this structure.

Figure 3-2

Figure 3-2. Absolute path of directory john

In 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. In other words, an absolute pathname always starts with a slash.

3.1.5. Relative Pathnames

You 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 pathname in the previous paragraph starts with a slash. That's what makes them relative pathnames! Relative pathnames start at the working directory, not the root directory. In other words, a relative pathname never starts with a slash.

3.1.5.1. Pathname puzzle

Here's a short but important question. The previous example explains 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.6. Changing Your Working Directory

Once you know the absolute or relative pathname of a directory where you'd like to work, you can move up and down the Unix directory tree to reach it.

3.1.7. Files in the Directory Tree

A 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

Figure 3-4. Files in the directory tree

Pathnames to files are made the same way as pathnames to directories. As with directories, files' pathnames can be absolute (starting from the root directory) or relative (starting from the working directory). 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.

Unix filesystems can hold things that aren't directories or files, such as symbolic links, FIFOs, and sockets (they have pathnames, too). You may see some of them as you explore the filesystem. We don't cover those advanced topics in this little book.

3.1.8. Listing Files with ls

To use the cd command, you must decide which entries in a directory are subdirectories and which are files. The ls command lists entries in the directory tree and can also show you which is which.

When you enter the ls command, you'll get a listing of the files and subdirectories contained in your working directory. The syntax is:

ls option(s) directory-and-filename(s)

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:

$ ls
$

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 screen should look something like this:

$ ls
ch1    ch10    ch2    ch3   intro
$

(Some systems display filenames in a single column. If yours does, you can make a multicolumn display with the -C [uppercase "C"] option or 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 showing a directory like the one in Figure 3-4:

$ ls -a
.      .exrc      ch1     ch2     intro
..     .profile   ch10    ch3 
$

When you use ls -a, you'll always see at least two entries with the names "." (dot) and ".." (dot dot). As mentioned earlier, .. is always the relative pathname to the parent directory. A single . always stands for its working directory; this is useful with commands like cp (see Section 4.4.2 in Chapter 4). There may also be other files, such as .profile or .exrc. Any entry whose name begins with a dot is hidden--it's listed only if you use ls -a.

To get more information about each item that ls lists, add the -l option. (That's a lowercase "L" for "long.") This option can be used alone, or in combination with -a, as shown in Figure 3-5.

Figure 3-5

Figure 3-5. Output from ls -al

The long format provides the following information about each item:

Total n
n amount of storage used by everything in this directory. (This is measured in blocks. On many systems, but not all, a full block holds 1024 bytes. A block can also be partly full.)

Type
Tells whether the item is a directory (d) or a plain file (-). (There are other less common types that we don't explain here.)

Access modes
Specifies three types of users (yourself, your group, all others) who are allowed to read (r), write (w), or execute (x) your files. We'll say more about this in a moment.

Links
The number of files or directories linked to this one. (This isn't the same sort of link as in a web page. We don't discuss filesystem links in this little book.)

Owner
The user who created or owns this file or directory.

Group
The group that owns the file or directory. (If your version of Unix doesn't show this column, add the -g option to see it.)

Size (in bytes)
The size of the file or directory. (A directory is actually a special type of file. Here, the "size" of a directory is of the directory file itself, not of all the files in that directory.)

Modification date
When the file was last modified, or the directory contents last changed (when something in the directory was added, renamed, or removed). If an entry was modified more than six months ago, ls shows the year instead of the time.

Name
The name of the file or directory.

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 system staff did it for you), this column should show your username. You also belong to a group, set by the person who created your account. Files you create are either marked with the name of your group, or in some cases, the group that owns the directory.

The permissions show who can read, write, or execute the file or directory; we explain what that means in a moment. The permissions have ten characters. The first character shows the file type (d for directory or - for a plain file). The other characters come in groups of three. The first group, characters 2-4, show the permissions for the file's owner, which is yourself if you created the file. The second group, characters 5-7, show permissions for other members of the file's group. The third group, characters 8-10, show permissions for all other users.

For example, the permissions for .profile are -rw-r--r--, so it's a plain file. The owner, john, has both read and write permissions. Other users who belong to the file's group doc, as well as all other users of the system, can only read the file; they don't have write permission, so they can't change what's in the file. No one has execute (x) permission, which should only be used for executable files (files that hold programs).

In the case of directories, x means the permission to access the directory--for example, to run a command that reads a file there or to use a subdirectory. Notice that the two directories shown in the example are executable (accessible) by john, by users in the doc group, and by everyone else on the system. A directory with w (write) permission allows deleting, renaming, or adding files within the directory. Read (r) permission allows listing the directory with ls.

You can use the chmod command to change the permissions of your files and directories. See Section 3.3, later in this chapter.

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 lists the directory but it does not change your working directory. The pwd command in the following example shows this:

$ ls -F /users/andy
calendar    goals    ideas/
ch2         guide/   testpgm*
$ pwd
/etc
$

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 to identify 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 (x), such as programs, are marked with an * (asterisk). The file testpgm is an executable file. Files that aren't marked are not executable.

ls -R ("recursive") lists a directory and all its subdirectories. This can make a very long list--especially when you list a directory near the root! (Piping the output of ls to a pager program solves this problem. There's an example in Section 5.2.3 in Chapter 5.) You can combine other options with -R: for instance, ls -RF marks each directory and file type.

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. 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 8--especially the man command for reading a command's online manual page.)

3.1.8.1. Exercise: exploring the filesystem

You're now equipped to explore the filesystem with cd, ls, and pwd. Take a tour of the directory system, hopping one or many levels at a time, with a mixture of cd and pwd commands.

Go to your home directory.

Enter cd

Find your working directory.

Enter pwd

Change to new working directory with its absolute pathname.

Enter cd /etc

List files in new working directory.

Enter ls

Change directory to root and list it in one step. (Use the command separator, a semicolon.)

Enter cd /; ls

Find your working directory.

Enter pwd

Change to a subdirectory; use its relative pathname.

Enter cd usr

Find your working directory.

Enter pwd

Change to a subdirectory.

Enter cd bin

Find your working directory.

Enter pwd

Give a wrong pathname.

Enter cd xqk

List files in another directory.

Enter ls /bin

Find your working directory (notice that ls didn't change it).

Enter pwd

Return to your home directory.

Enter cd



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.