Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Symbolic links, paths, and rsync's -l option UNIX
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.
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[26,659 views]  

Symbolic links, paths, and rsync's -l option | 3 comments | Create New Account
Click here to return to the 'Symbolic links, paths, and rsync's -l option' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Symbolic links, paths, and rsync's -l option
Authored by: LC on Apr 22, '05 01:41:03PM
Note: rsync's -a (same as --archive) option turns on the -l switch, i.e. it implies/includes/activates -l. Most often when folks use this utility, the -a is chosen (my second-most-used options have been -H and -u).

Also, in general when we create symlinks, they should always be composed relatively; the only exception to that in my experience has been a symlink which needs to refer to a different volume (filesystem) ... in which case it's got to be absolute (an NFS-rooted pathname, etc.) Larry. (Note: for OS X we need to have the resource-fork-capable rsync such as hfsrsync; http://hfsrsync.darwinports.com)

[ Reply to This | # ]

rsyncx
Authored by: xdavid on Apr 22, '05 04:02:04PM

Is hfsrsync different again from rsyncx?



[ Reply to This | # ]
rsyncx
Authored by: LC on Apr 27, '05 02:25:39PM
I guess not -- they seem to use the same source code (at OpenDarwin) for rsync, although I believe these are different packaging styles (i.e. the installation and package file placement). I didn't see a page for rsyncx under the new blog format at macosxlabs.org ... but their rsyncx main page is still available at this page.

For more basic (non-sync) transfers (including the resource forks), I use ditto --rsrc or sometimes Helios xtar. And, I'm setting up netatalk on my Linux system at home, to make some sort of AFP-geared backup system; Larry.

[ Reply to This | # ]