Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Creating Symbolic Links

Create symbolic links using the ln command with the -s option. The syntax is as follows:

ln -s source destination

Here, source is either the absolute or relative path to the original version of the file, and destination is the name you want the link to have.

For example, the following command

$ ln -s /home/httpd/html/users/ranga /home/ranga/public_html

creates a link in my home directory to my Web files. If you encounter an error while creating a link, ln will display an error message. Otherwise, it displays no output.

In this example, you used absolute paths. In practice, relative paths are preferred for the source and the destination. For example, the actual commands I used to create a link to my Web files are the following:

$ cd
$ ln -s ../httpd/html/users/ranga ./public_html

You can see the relative path by using ls -l:

$ 1s -1 ./public_html
lrwxrwxrwx   1 ranga   users   26 Nov  9  1997 public_html -> ../httpd/
⇒html/users/ranga

This output indicates that the file is a link and also shows the file or directory that the link points to.

Common Errors

The two most common errors encountered when creating symbolic links happen when

  The destination already exists.
  The destination is a directory.

If the specified destination is a file, it does not create the requested link. For example, if the file .exrc exists in my home directory, the command

$ 1n -s /etc/exrc .exrc

produces the following error message:

ln: cannot create .exrc: File exists

If the specified destination is a directory, ln creates a link in that directory with the same name as the source. For example, if the directory pub exists in the current directory, the following command

$ In -s /home/ftp/pub/ranga pub

creates the link pub/ranga rather than complaining that the destination is a directory. I mention this behavior of ln as a common error because forgetting about that fact is a common shell script bug.

Device Files

You can access UNIX devices through reading and writing to device files. These device files are access points to the device within the file systems.

Usually, device files are located under the /dev directory. The two main types of device files are

  Character special files
  Block special files

Character Special Files


Character special files provide a mechanism for communicating with a device one character at a time. Usually character devices represent a “raw” device. The output of ls on a character special file looks like the following:
crw-------   1 ranga    users      4,   0 Feb  7 13:47 /dev/tty0


The first letter in the output is c, therefore you know that this particular file is a character special file, but you also see two extra numbers before the date. The first number is called the major number and the second number is called the minor number. UNIX uses these two numbers to identify the device driver that this file communicates with.

Block Special Files


Block special files also provide a mechanism for communicating with device drivers via the file system. These files are called block devices because they transfer large blocks of data at a time. This type of file typically represents hard drives and removable media.

Look at the ls -l output for a typical block device. For example, /dev/sda:

brw-rw----   1 root     disk       8,   0 Feb  7 13:47 /dev/sda

Here the first character is b, indicating that this file is a block special file. Just like the character special files, these files also have a major and a minor number.

Named Pipes


One of the greatest features of UNIX is that you can redirect the output of one program to the input of another program with very little work. For example, the command who | grep ranga takes the output of the who command and makes it the input to the grep command. This is called piping the output of one command into another. You will examine input and output redirection in great detail in Chapter 13, “Input/Output.”

On the command line, temporary anonymous pipes are used, but sometimes more control is needed than the command line provides. For such instances, UNIX provides a way to create a named pipe, so that two or more process can communicate with each other via a file that acts like a pipe. Because these files allow process to communicate with one another, they are one of the most popular forms of interprocess communication (IPC for short) available under UNIX.

Sockets


Socket files are another form of interprocess communication, but sockets can pass data and information between two processes that are not running on the same machine. Socket files are created when communication to a process on another machine located on a network is required. Internet tools in use today, such as Web browsers, use sockets to make a connection to the Web server.

Owners, Groups, and Permissions

File ownership is an important component of UNIX that provides a secure method for storing files. Every file in UNIX has the following attributes:

  Owner permissions
  Group permissions
  Other (world) permissions

The owner’s permissions determine what actions the owner of the file can perform on the file. The group’s permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file. The permissions for others indicate what action all other users can perform on the file.

You can perform the following actions on a file:

  Read
  Write
  Execute


If a user has read permissions, that person can view the contents of a file. A user with write permissions can change the contents of a file, whereas a user with execute permissions can run a file as a program.


Previous Table of Contents Next