3

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.

Another thing can happen, if the file and the link have the same name, the link should have a different name automatically too...

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

7
  • en.wikipedia.org/wiki/UnionFS could be worth considering, but I dont know your specific use-case. If you are into programming look for a "directory walk" function of some sort. I think it will get a little complicated. – sleeplessnerd Oct 16 '11 at 23:47
  • Hum...thx about the answer, unfortunally, I would like a program to use in ext4 filesystem. Do you know something like that to ext4? – GarouDan Oct 17 '11 at 0:13
  • 1
    This is a good question, but I think it should be better answered if you ask it in ServerFault. – Herberth Amaral Oct 17 '11 at 1:25
  • Question also exists on Unix&Linux – glenn jackman Oct 17 '11 at 13:07
  • My first thought would have been cp -rpn /B/* /A and then delete /B and finally, ln -s /A /B. But I know you're looking for something more complicated (perhaps the directories are in different file systems?). – Belmin Fernandez Oct 18 '11 at 18:23
0

As Arcege answer to me in Unix&Linux the answer is below.

Thx very much Arcege!

If someone improve a bit more this script, please post an answer too =).

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..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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