Hard and Symbolic Links

1. What's the difference between ln and ln -s?

1. What’s the difference between ln and ln -s?

ln is a useful command that provides all of the advantages of copying, but without the need for additional storage space and without some of the headaches, like maintaining multiple instances of the file.

Like cp, ln lets you propagate a file into different locations. But instead of making a physical copy of the file, ln makes a link to the file. Thus, all “copies” of a file simply refer to a single file, and changing any copy changes all copies.

Linking is a very useful technique. For example, if your web site goes from a set of static files to a dynamic, database-driven site, you can use links to redirect your visitors to a search function to find the old material in your new system. With potentially hundreds of files to refer back to the search engine, one file, with many links, saves quite a bit of space. And, if you need to make change, editing the “master” file effectively updates all files.

File name aliasing is another common use for links. A script may change its behavior depending on its name. For example, fmt is also named cfmt and refmt. No matter what its name is, fmt reformats text to fit in 65 columns or less. However, if it’s invoked as cfmt, it expects script comments to start with # (the “pound” or “hash” symbol), and if it’s invoked as refmt, it expects email quotes that begin with =>.

So, how does ln work? You invoke ln as ln old_file new_ file. (For the moment, don’t worry about the -s option.) ln creates a new directory entry (a second entry) pointing to old_name, but gives it a new name, new_name.

In the case of fmt, which usually lives in /usr/local/bin, the system administrator (or an install script) created its two aliases with:

# cd /usr/local/bin
# ln fmt cfmt
# ln fmt refmt

To see what’s happening “under the hood,” use the command ls -i *fmt to see the i-node number associated with each file. Normally, each file has a unique i-node number, but in the case of a link, it has the same i-node number of the file it’s linked to. Instead of three files taking up disk space, the system maintains one file with three names.

You can use ln to create links almost anywhere — the only limitation is that all of the links must exist in the same physical filesystem. Also, links created with ln cannot link to directories, just files.

The ln -s or symbolic link option works a bit differently. -s doesn’t create a link to a file by creating a new name. Instead, it creates a new, but small file that names the file it’s linked to. If you use the ls -i command, you’ll see different i-node numbers.

Using the fmt example, if you installed it and then executed the commands…

# ln -s fmt cfmt
# ln -s fmt refmt
# ls -i *fmt

ls -i would show three different i-node numbers, indicating that there are three separate different files. So, what’s ln -s good for? It lets you create links that cross filesystems.

You might think of ln as “link by i-node,” and ln -s as “link by name.”

Is FreeBSD the same thing as Linux?

FreeBSD, and its siblings, OpenBSD and NetBSD, are not the same as Linux. While the three BSD variants are free like Linux, and each provides a Linux compatibility interface, the BSD kernel represents a different approach to operating systems design. Not better or worse than Linux, just different.

OpenBSD prides itself on its emphasis on security. OpenBSD is thought of by many security professionals to be the most secure UNIX-like operating system. It’s ideal for building firewalls and private network services.

NetBSD touts to be the strongest in networking, and also claims to have wide platform support on the latest high-end hardware, such as Alpha and PowerPC. NetBSD is also very secure. It has the least number of security bugs reported in any public forums.

FreeBSD claims to have the most leading-edge features. It also claims to provide the most robust network services under the heaviest loads, using memory efficiently to maintain good response times for thousands of simultaneous user processes.

If you’re trying to decide between Linux and one of the BSD projects, it’ll take some homework to identify the strengths and weaknesses of each system to determine which is right for you. For the general user, however, there is little difference. For the developer, system administrator, or power user, there may be worlds of difference.

John R. S. Mascio is an IT consultant, specializing in Linux and Open Systems. He can be reached at mascio@ryu.com.

Comments are closed.