Working with Link Files
As we
discussed earlier in this chapter, the Linux file system supports a file type
called a link file.
Link files don’t contain content in
the way that regular files do. Instead, they are redirectors that point you to
a different file or directory in the file system. On Linux you can create two
different types of link files:
• Hard
A hard link is a file that points
directly to the inode of another file. An inode stores basic information about
a file in the Linux file system, including its size, device, owner, and permissions.
Because the two files use the same inode, you can’t actually tell which file is
the pointer and which is the pointee after the hard link is created. It is as
if the two files were the same file, even though they may exist in different
locations in the file system.
• Symbolic
A symbolic link file also points to
another file in the file system. However, a symbolic link has its own inode.
Because the pointer file has its own inode, the pointer and the pointee in the
file system can be easily identified. For example, in the preceding chapter,
you saw that the /usr/bin/vi file is actually a symbolic link that points to
the /bin/vim file. This is shown in the following example:
openSUSE:/usr/bin # ls -l vi
lrwxrwxrwx 1 root root 8 Jan 19
10:24 vi -> /bin/vim
To create a link file, you use the
ln command. The syntax is
Ln <pointee_file>
<pointer_file>
. Using ln without any options
creates a hard link. If you want to create
a symbolic link, you use the –s option. In the example that follows, the ln
command has been used to create a symbolic link between a file named myapp in
the bin subdirectory of my user’s home directory and an executable file named
myapp located in /var/opt:
rtracy@ openSUSE:~/bin> ln –s /var/opt/myapp
myapp
rtracy@ openSUSE:~/bin> ls -l
total 0
lrwxrwxrwx 1 rtracy users 14
2011-02-01 13:15 myapp -> /var/opt/myapp
rtracy@fs2:~/bin>cd ..
rtracy@fs2:~> myapp
This is my new executable script
file.
Using the ls –l command, you can see that myapp actually points to the
/var/opt/myapp executable file. This can be a useful trick if the
/var/opt/myapp program is one that you need to use frequently. Because the
/var/opt directory is not in my user’s PATH environment variable, I would need
to supply the full path to the myapp executable each time I wanted to run it
(or switch to the /var/opt directory first). However, the bin subdirectory of
my home directory is automatically added to the PATH environment variable by my
bash configuration files. By placing the symbolic link in this directory, I can
run the /var/opt/myapp executable by simply entering myapp at the shell prompt.
The symbolic link file redirects the shell to the appropriate file in /var/opt
and runs it from there. This is shown in the preceding example.
No comments:
Post a Comment