Symbolic links, paths, and rsync's -l option

Apr 22, '05 09:55:00AM

Contributed by: kd4ttc

rsync has an option, -l which will cause it to copy symbolic links (symlink for short). This hint explains how to set up symbolic links so that they work in the location that rsync has transferred your files to. Symbolic links, created with the ln -s, command are just paths. The man page for ln does not explain this well. Try executing the command ln -s /A/random/path ~/px, and you will find a symlink in your home directory named px. ls -l on your home directory will show this line:

lrwxr-xr-x   1 stephenh  staff    14 19 Apr 21:01 px -> /A/random/path
You can create any path you want -- ln does not check for a file's existence when the path is entered. Thus the ln -s command should be documented as:
  ln -s[fhinsv] path file
	
  path - path to file or directory to link to
  file	file to hold the link
I have a hierarchy of folders and files that store the medical records for my practice. Occasionally a patient gets a new name, generally by marriage, and it is helpful to have the folder accessible by both the old name and the new name. Initially I didn't understand what ln was doing with symbolic links, so I was using absolute paths. This worked in my medical records, but when rsync was used to archive the records, and the link was used within new copy, the links were broken. The solution was to use relative paths rather than absolute paths.

For example, my records were stored in /Users/sholland/MedicalRecords/. With rsync, the records copied to another computer at /Users/Shared/MedicalRecords/. A link in MedicalRecords made as follows:

ln -s /Users/sholland/MedicalRecords/S/SmithJane/ /Users/sholland/MedicalRecords/J/JonesJane
created a useful symlink where JonesJane pointed to SmithJane, but after the transfer to the new computer, the link was broken in the new location. The fix was to create the link as follows in the original medical record system:
ln -s ../S/SmithJane/ /Users/sholland/MedicalRecords/J/JonesJane
or if you did a cd to the J directory, you could create the same file with:
ln -s ../S/SmithJane/ JonesJane
When this link is copied by rsync, it works on either computer. This link will work when the link is in any directory A through Z in the above example, because it gets to the correct place by moving up one level then descends into the S directory and points to the SmithJane directory.

However, if the symlink file is moved anywhere else, the link will be broken. The reason it is useful here is that the whole MedicalRecords hierarchy is moved intact. A symbolic link that had a full path specification would not break wherever it was moved, as long as the full path was valid. In my application, the fully-specified path broke because the MedicalRecords directory had a different root; with the path to MedicalRecords being different on the different computers, a relative link was needed.

What I learned from all this is that the symbolic link is just a path stored in a file which has special characterists so that the file system will use the link to get to the file or directory you want. If the link will exist within a directory hierarchy that will be moved about, a relative type of link should be specified. If the link is pointing to a thing that never changes location, a full path specification will work, even if the symlink is moved to any directory.

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050419230722397