Understanding File and Directory Concepts

In Solaris, data is stored within a file system located on a hard disk. A file system is simply a logical unit of space on the disk and is divided into two basic components: files and directories. Files contain data, and a directory is a specialized file that lists filenames.

Think of a hard disk as a filing cabinet. The purpose of a filing cabinet, of course, is to store files. Filing cabinets have drawers, which are analogous to file systems. Some cabinets have more drawers than others, and the same can be true of file systems on a hard disk. Inside the drawers, there are folders. The folders play the role of directories in a file system. Inside the folders are your documents. These are files themselves.

Reviewing File Types

Solaris 9 employs eight types of files. The most common type is a "regular" text or application file. The file types supported in Solaris are shown in Table 5.1.

Table 5.1: Solaris File Types

Symbol

Meaning

-

Text or application

D

Door

d

Directory

b

Block special file

c

Character special file

p

Named pipe

l

Symbolic link

s

Socket

Solaris regards almost everything on your system as a file. Documents and programs are files. Commands are essentially programs and are therefore files. Directories are files that contain other files. Even devices on your computer, such as your hard drive, terminal, and printer, are considered files.

To show a listing of files in a directory, use the ls command. The ls command has 26 arguments that you can use to customize your display. The most commonly used options for ls are -l (long listing) and -la (long listing and including files that start with a period, which are usually hidden). For a complete listing of ls switches, type man ls. Here is a sample directory listing:

 $ ls -l total 7 -rw-rw-r-- 1 root     other       35068 Aug  4 13:39 bookdoc drwxr-xr-x 2 root     other         512 Aug  3 10:01 dir1 drwxr-xr-x 1 root     other         512 Aug  3 10:02 dir2 -rwxrwxrwx 1 root     other      188326 Aug  2 19:11 fungame -rw-r--r-- 1 root     other        4105 Aug  1 12:21 textfile $ 

The long listing of a directory shows seven fields. On the left, you see the file's permissions. The d in front of dir1 and dir2 indicates that they are directories. The number after the permissions indicates the number of links to the file. After the links, you see the owner of the file (root in this case) and the primary group of the owner. The file size in bytes, last modification date and time, and filename end each entry.

Using the File System

Solaris file systems are organized in a hierarchical tree. The base of this tree, or the root directory, is called root and is represented with a slash (/). All other files and directories can be thought of in terms of their relationship to the root. Figure 5.1 illustrates this hierarchical structure.

click to expand
Figure 5.1: Solaris hierarchical file structure

Note 

The superuser account and the base of the directory structure are both named root. This can be confusing to new administrators. To avoid potential confusion, be sure to specify "the root directory" or "the superuser."

One of the basic tenets of file systems is that each object must have a unique absolute pathname. For example, consider the /etc directory. This directory named etc exists right off of the root (/). Therefore, you cannot create another directory named etc from within the root directory. However, if you were to switch to the /export directory, you could create a directory called etc. The absolute pathname of this new directory would be /export/etc. Also remember that Solaris is case sensitive; the directory /export/dir1 is different from /export/DIR1.

File System Navigation

To determine where you are in the file system, use the pwd (print working directory) command. Your working directory is the first directory checked by the kernel when you enter a command to execute (if you have . as your $PATH variable) or want to see a directory listing. The working directory is also known as the current directory.

Warning 

Using . in your path can compromise system security. This path designation can cause you to inadvertently execute a program you did not intend to, such as a Trojan horse.

Warning 

Don't confuse the working directory with the home directory. Your home directory is used to store personal files, whereas the working directory is the directory you are currently working in. Your working directory might or might not be your home directory. You can change working directories easily with the cd command, but your home directory does not typically change.

To illustrate the importance of knowing your working directory, let's look at creating a directory with the mkdir command. Say you execute the following command sequence:

 $ ls file1 file2 $ mkdir dir1 $ ls dir1 file1 file2 $ 

You have just created a directory called dir1. Congratulations! You log out of Solaris and go home for the day. The next day when you get to work, you can't seem to find the dir1 directory. But you created it, right? So where is it? That's a good question.

Unfortunately, you don't remember what your working directory was when you created dir1. So although you will likely be able to find dir1 eventually, it might not be where you think it is. To help, you could have (and should have) used pwd to verify that you were in the right directory before creating a new one.

Relative Pathnames

A relative pathname indicates the location of a file based on your current working directory. Any pathname that does not begin at the root directory, or with a tilde (~), is a relative pathname. Absolute pathnames never change, but relative pathnames do, based on where you are in the directory tree structure.

Say that you want to create a directory called /export/home/user1/files. There are a couple of ways you can go about it. One option is to create it from the root:

 $ pwd / $ mkdir /export/home/user1/files $ ls /export/home/user1 files $ 

To create this directory, you type in the absolute pathname. But that is a lot of typing, and it's easy to make a mistake when you type that much. If you are user1, it's probable that your home directory is /export/home/user1, and just as likely that it's your default working directory as well.

 $ pwd /export/home/user1 $ mkdir files $ ls files $ 

Both command sequences create the same directory. However, the first one uses an absolute pathname, and the second one uses a relative pathname. Although using relative pathnames can save you time, using them also increases the probability of creating a directory in the wrong place. If you're going to use relative pathnames, make sure to use pwd to verify your working directory.

The . and .. Directories

Each directory automatically has two entries: a single period and a double period. The single period represents the directory itself, and the double period represents the directory's parent directory. When you use the ls command, you typically don't see these entries, because they begin with a period, and by default ls doesn't show entries that begin with a period.

Note 

The double period can be especially useful as a shortcut when copying or moving files, and when using utilities that require filenames or pathnames.

Useful File Commands

There are literally dozens of commands you can use to create, copy, move, rename, display, and modify files. Although listing them all here would be impractical, a quick review of some common commands doesn't hurt. Table 5.2 lists some common file-management-based commands.

Table 5.2: File Commands

Command

Description

cat

Concatenates and displays files. For example, cat file1 file2 >file3 combines files file1 and file2 in a new file named file3. The command cat file1 simply displays the contents of file1.

cd

Changes directories. For example, cd /export/home/user1 changes the working directory to /export/home/user1. The command cd .. moves up one directory in the tree, and cd / changes to the root directory.

cp

Copies files. The command cp file1 file2 creates a copy of file1 named file2. For copying directories, use cp -r, which copies the directory as well as any files or subdirectories to the desired destination.

file

Determines file type.

find

Finds files.

ls

Lists files in a directory. If no directory is specified, the working directory is used.

mkdir

Creates directories. The command mkdir dir1 creates a directory called dir1 in the current working directory.

more

Filter that enables you to display one screen of information at a time. For example, to see your /etc/passwd file one page at a time, use more /etc/passwd.

mv

Moves or renames files. The command mv file1 file2 renames file1 to file2. The command mv file1 /etc/acct moves file1 to the /etc/acct directory.

pwd

Displays the present working directory.

rm

Removes (deletes) a file. To delete file1, use rm file1.

rmdir

Removes empty directories. If the directory has files in it (other than . and ..), delete the files first and then use rmdir to remove the directory.

Warning 

The rm -r command can be used to remove a directory and all files within the directory. This is an incredibly powerful command, and you should use care when executing it. Just think about what would happen if you ran this command from the root directory.

One other handy navigation shortcut is the tilde (~). The tilde is used as a shorthand representation of the user's home directory. To change to his home directory quickly, a user can use cd ~. To create a directory called myfiles in her home directory, a user could execute the mkdir ~/myfiles command, regardless of her working directory.

Note 

The ~ shortcut does not work in the Bourne shell. To reference the home directory in the Bourne shell, use the $HOME variable.

Modifying File Characteristics

When you use the ls -l command to display files in a directory, you see seven fields of information about the file. The fields are permissions, links, owner, group, size, modification date and time, and filename. Here is an example:

 $ ls -l total 3 drwxrwxr-x  2 root     other       512 Aug 10 11:23 docs 

This section shows how to modify file characteristics other than permissions and links. Links are covered two sections from now, and security is covered in the second half of this chapter.

The owner of the example directory is root. This is very important, considering that the base set of security permissions relates specifically to the owner of the file. To change owners, use the chown command:

 # ls -l total 3 drwxrwxr-x  2 userx     other       512 Aug 10 11:23 docs # chown qdocter docs # ls -l total 3 drwxrwxr-x  2 qdocter   other       512 Aug 10 11:23 docs 

The ownership group can also be changed, but that is done with the chgrp command:

 # ls -l total 3 drwxrwxr-x  2 qdocter     other       512 Aug 10 11:23 docs # chgrp author docs # ls -l total 3 drwxrwxr-x 2 qdocter      author      512 Aug 10 11:23 docs 

The file size attribute isn't something you typically set out to purposely modify. It's primarily there for reference. If someone is storing incredibly large files on your server, you can see who it is. If you do want to modify the size of a file, open the file, make some changes to it, and save it.

The modification date can be updated in the same way. Open the file, make changes, and save the file. However, the touch command can also be used to update the modification date and time without actually opening the file. If you use touch with a filename that does not exist, touch will create the file specified:

 $ ls -l total 3 drwxrwxr-x  2 root     other       512 Aug 10 11:23 docs # touch file1 # ls -l total 4 drwxrwxr-x  2 root     other       512 Aug 10 11:23 docs -rw-r--r--  1 root     other        80 Aug 10 11:44 file1 # touch docs # ls -l total 4 drwxrwxr-x  2 root     other       512 Aug 10 11:45 docs -rw-r--r--  1 root     other        80 Aug 10 11:44 file1 

Notice how the touch command updated the time on the docs directory and created an empty file named file1.

Using Default Directories

During installation, Solaris creates several default directories. These directories provide the basic organization of the Solaris file system and group similar types of files together. Remember that the file system is a hierarchical tree. The main part of the tree is the root, and the branches expand from there. Table 5.3 lists the major default directories and their intended purposes.

Table 5.3: Solaris 9 Default Directories

Directory

Purpose

/ (root)

The top of the hierarchical file system tree. Contains directories and files critical to system operation.

/dev and /devices

Device files that represent configured devices in the system. The /dev and /devices directories are discussed in more detail in Chapter 6, "Disk and Device Management."

/etc

Machine-local configuration files. Some of the more notable files include /etc/passwd and /etc/shadow.

/export

Exported files. Typically, user home directories are located in /export/home/username.

/opt

Optional mount point, generally used for third-party applications.

/tmp

Temporary files and oftentimes swap space.

/usr

System files and directories that can be shared among users. The /usr/bin directory contains user commands, /usr/sbin holds system administration commands, and /usr/share contains man pages.

/var

Variable data, which includes files that change dynamically as the system runs. Log files, printer spool files, and backup files are often located here.

To run a system, the root and /usr directories are required, at a minimum. Your system will likely have all the directories listed in Table 5.3 and more.

Working with Links

As you know by now, files are stored on hard disks in computers. You also know that files have names, which is how humans reference files. On the computer, the filename is linked to the data. In other words, the filename points to the specific location on the hard drive where the data is stored. In Solaris, it's possible to create multiple links that point to the same file.

Hard Links

A hard link is a pointer to a file. Bear in mind that a hard link is not a copy of a file, but rather a pointer to the original file. In other words, if you have seven hard links to one file and change the data in the file, all links will reflect the changed data as well.

All hard links to a file are of equal value to Solaris. If a particular file has three hard links, no one link has priority over the others. You could delete any two of the links and still access the data in the file through the third (and now only) link. If all hard links to a file are deleted, then the data is inaccessible, and the disk space is made available for other files.

So why would someone want to create hard links? Imagine that on your computer, you have five files that you commonly access. Each file is in a different directory. To open each file, you need to remember the specific directory path to each one. Granted, five isn't a terribly large number of paths to remember, but it's inconvenient nonetheless. Because of a controlling manager, all files must remain in their original directories.

To get around this, you could create a directory specifically for links or use an existing directory. Then create links in your chosen directory that point to all five files. Now, all five access points (to the data you need) are located in the same directory. Even better, the original links to the files are still intact and in their original directories, pleasing the controlling manager.

Another reason to create a link is to give another user access to a file that's in your home directory. The other user can create a link in her home directory pointing to the file in your home directory, and both of you have convenient access to the file. When you create a link, the original permissions remain intact, so you might have to modify file and/or directory permissions to get the access permissions you need. However, that's a topic for later in this chapter.

Links are created with the ln command. The syntax is as follows:

 $ ln file link 

where file is the path to the existing file, and link is the name of the link that you wish to create.

Here's an example of creating a link:

 $ ls file1 file2 $ ln file1 link1 $ ls link1 file1 file2 $ ln file1 seclink $ ls seclink link1 file1 file2 

By looking at a basic directory listing, it's practically impossible to tell which files are links. The ls -i command helps shed more light on the subject:

 $ ls -i    5511 seclink   5511 link1  5511 file1  242176 file2 $ 

When you use ls -i, the inode of the file is displayed. The inode contains the complete listing of all information about a file: its owner, group, permissions, last access time, and so on. Another way to look at it is that the inode points out the unique location of the data on the hard disk. In the example, seclink, link1, and file1 all have the same inode, meaning they all reference the same data. The other file, file2, is obviously a different file because it has a different inode.

There are two major limitations to using hard links. First, hard links must be created to files and cannot be created to directories. Second, hard links must be in the same file system as the original file. This is because inodes are file-system-dependent. The 5511 inode in one file system will contain different data than the 5511 inode in another file system.

Soft Links

To address the limitations of hard links, soft links (also known as symbolic links) were created. A soft link is more flexible than a hard link; it can be used to link to a directory and can span multiple file systems. Soft links are created with the ln -s command.

Whereas hard links are direct pointers to a file, soft links are indirect pointers to a file. All a soft link contains is the absolute pathname to the linked file. Consequently, soft links can point to nonexistent files, and hard links cannot. Of course, if you open a soft link that is pointing to an imaginary file, you're not going to get the information you're looking for. If you're using hard links, you can delete the original file and still access data through another link. The same is not true of soft links. If you delete the original file, the data is gone, and the link becomes useless.

To delete a link, you use the rm command; it's the same command you use to delete regular files.




Solaris 9. Sun Certified System Administrator Study Guide
Solaris 9 Sun Certified System Administrator Study Guide
ISBN: 0782141811
EAN: 2147483647
Year: 2003
Pages: 194

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