Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm searching a tool or a shell script or another program to synchronize two or more folders or disks.

But it differs of rsync tool, because, I don't want to duplicate the missing file, but do a symbolic link of it in the other folder, for example:

If in folder A we have the files a and b and other folder c with a file d:

A

a

b

c

d

And in folder B we have the file 1 and a folder 2 with a file 3:

B

1

2

3

I would like program, for example, ssync (symbolic synchronize, just a invention name), called by a command, for example, ssync /A /B to change these folders like showed below:

A

a

b

c

d

1 (ln -s)

2 (ln -s)

3 (ln -s)

and

B

1

2

3

a (ln -s)

b (ln -s)

c (ln -s)

d (ln -s)

where ln -s indicates that in this folder was created a symbolic link to this corresponding file, for example, a (ln -s) indicates the command ln -s /A/a /B/a was used.

I will appreciate so much if someone help me. Thanks.

share|improve this question
    
Have you looked into rsnapshot, which creates hard links? I won't be using symlinks to synchronize anything. Access/modification times are hard to duplicate with symlinks. Attempts to change the atime of a symlink often result in "Permission denied" errors, even if you own the symlink. –  Arcege Oct 16 '11 at 23:58
    
Hard links it's really a good idea. But do you have a ideia to do this with to two folders (with another folders in), synchronizing all files creating hard links? I am interesting in this rsnapshot package but it isn't familiar to me, so if you write a command explaining your idea I would like so much. Thanks! –  GarouDan Oct 17 '11 at 0:18
    
I was reading about the hard links in rsnapshot, but looks like it create and uses a hard link only if a file isn't modificated...looks like I can't create a hard link instead copy the file (in the first running synchronization), but I'd would like create the hard link...never the entire file... could I do this with this rsnapshot or another tool? –  GarouDan Oct 17 '11 at 0:32
    
rsnapshot (rsnapshot.org) is not standard, but it builds on top of rsync. I've re-engineered it in Python to include in an install program to handle months of backups with minimal space. I sync entire trees, not just a single directory. –  Arcege Oct 17 '11 at 1:04
    
Sorry, I thought there was a mode to sync two directory trees instead of just making multiple revisions of the first tree. I've been using my own version for a few years. –  Arcege Oct 17 '11 at 1:15
add comment

1 Answer 1

up vote 2 down vote accepted

Try this:

#!/bin/sh
srcdir="$1"
dstdir="$2"
if [ -h "$srcdir" ]; then
    val=`readlink "$srcdir"`
    if [ ! -h "$dstdir" ]; then
        rm -rf "$dstdir"
    fi
    ln -s "$val" "$dstdir"
elif [ -d "$srcdir" ]; then
    if [ ! -d "$dstdir" ]; then
        rm -rf "$dstdir"
    fi
    mkdir "$dstdir"
    for i in `ls -1A "$srcdir"`; do
        $0 "$srcdir/$i" "$dstdir/$i"
    done
    touch -a -r "$srcdir" "$dstdir"
    touch -m -r "$srcdir" "$dstdir"
else
    if [ ! -f "$dstdir" ]; then
        rm -rf "$dstdir"
    fi
    ln "$srcdir" "$dstdir"
fi

This doesn't handle "special" files like block devices, named pipes, sockets, etc..

share|improve this answer
    
thx a lot about your script. I will investigate it. Really thx about your time. –  GarouDan Oct 17 '11 at 11:55
    
Thx very very much... I need learn a bit more to understood all your code but it works perfectly. Thx. Unfortunally I didn't have 100 points or more to give you...or vote up +10 in your answer...I think this can help a lot of people. For example...if you want creta a folder with links to all files do you have in all partitions you have...to movies, texts, pdfs, musics, etc. –  GarouDan Oct 17 '11 at 12:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.