Linking with ln (Hard Links)


Linking with ln (Hard Links)

Suppose your boss just hired an assistant for you ('bout time, right?). You'll need to make sure your new helper can access your files so you can pawn off your work on him. And, you'll need to access the revised files just so you can keep up with what your helper's been doingand perhaps take credit for his work at the next staff meeting.

A great way to give your helper easy access to your files is to create a hard link from your home directory. In making a hard link, all you're doing is starting with an existing file and creating a link, which (sort of) places the existing file in your helper's home directory. The link does not create a copy of the file; instead, you're creating a second pointer to the same physical file on the disk. Rather than the additional pointer being secondary (like an alias or shortcut in Macintosh or Windows computers), both of the pointers reference the same actual file, so from the perspective of the Unix system, the file actually resides in two locations (Code Listing 2.14).

Because using hard links often requires that you have access to another user's home directory, you might venture to Chapter 5 for details about using chmod, chgrp, and chown to access another user's directories and files.

Code Listing 2.14. Hard links let two users easily share files.

$ ls /home/deb/Projects/schedule/our* /home/helper/our* ls: /home/helper/our*: No such file or directory /home/deb/Projects/schedule/our.projects.latest /home/deb/Projects/schedule/our.projects.other $ ln /home/deb/Projects/schedule/our.projects.latest /home/helper/our.projects $ ls -l /home/helper/o* -rw-rr    3 ejr      users     1055 Jun 26 11:00 /home/helper/our.projects $ 

To Make a Hard Link:

1.

ls -l /home/deb/Projects/schedule/ our* /home/helper/our*

To begin, list the files in both directories to make sure that the file to link exists and that there's no other file with the intended name in the target directory. Here, we list the files that start with our in both /home/deb/Projects/schedule and in /home/helper. In this example, we're verifying that the file does exist in Deb's directory and that no matching files were found in the helper's directory (Code Listing 2.14).

2.

ln /home/deb/Projects/schedule/ our.projects.latest /home/helper/our.projects

Here, ln creates a new file with a similar name in the helper's home directory and links the two files together, essentially making the same file exist in two home directories.

3.

ls -l /home/helper/o*

With this code, your helper can verify that the file exists by listing files that begin with o*.

Now the file exists in two places with exactly the same content. Either user can modify the file, and the content in both locations will change.

Tips

  • You can remove hard links just like you remove regular files, by using rm plus the filename. See the section called Removing Files with rm in this chapter.

  • If one user removes the file, the other user can still access the file from his or her directory.

  • Hard links work from file to file only within the same file system. To link directories or to link across file systems, you'll have to use soft links, which are covered in the next section.

  • If you're sneaky, you can use hard links to link directories, not just files. Make a new directory where you want the linked directory to be, and then use ln /home/ whoever/existingdirectory/* /home/ you/newdirectory/ to hard link all of the files in the old directory to the new directory. New files won't be linked automatically, but you could use a cron job to refresh the links periodicallysay, daily. See Chapter 9 for cron details.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

Similar book on Amazon

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