Tuesday, July 20, 2010

Moving files with symbolic link vs. cross-filesystem

In linux, I mounted a raided lvm in /mnt/raid/home and bind it to /home. I also have /mnt/raid/shared/Music and other directories to be shared across users. Within my personal directory /home/username, there are some a few symbolic links to /mnt/raid/shared/Music and other shared directories.

This way, my media files can be accessed by all local users -- it also makes backing up of personal files (as opposed media files) easier.

The problem is, when moving files in or out of those shared folders, Linux mv can not resolve the complicated symbolic path -- what it sees are files across file systems. Therefore, a simple mv is translated into cp and rm, rather than simply updating the node info.

The solution is to translate everything to absolute path. Here's a simple script to do that.


#!/usr/bin/perl

use Cwd 'abs_path';

foreach my $arg (@ARGV)
{
    next if ($arg =~ /^-/); # skip switches
    $arg = abs_path($arg);
    $arg =~ s|^/home/|/mnt/raid/home/|;
}

system("/bin/mv", @ARGV);

This alone is enough for my environment. You may want to have more substitution rules for a more complex setting.