3

I have a Documents file in my home directory as usual. Now in my dropbox folder I have created a new directory named Documents. Now is there any way such that this two could be same? i.e if I update any of this, both of them will be updated. I think it may be done by symlink, but the idea of symlinks is not clear to me about what it does.

terdon
  • 100,812
Unbound
  • 117
  • 1
  • 9

3 Answers3

1

Just use symlinks:

ln -s ~/Documents ~/Dropbox/

That will create a directory in your $HOME/Dropbox that is actually a link to ~/Documents. This means that any changes you make to ~/Documents will also be visible in ~/Dropbox/Documents since the latter is just a link to the former.

Symlinks are simple tricks. You can think of the symlink as a virtual copy of the link's target. Any operations done on the link are actually applied to the target. The two are exact copies of each other. Deleting the link will not affect the target in any way. To illustrate, here's a simple example:

$ mkdir foo
$ touch foo/file1
$ tree
.
└── foo
    └── file1

1 directory, 1 file

So, we have a directory called foo that contains a file called file. Now, what happens if we create a link to foo?

$ ln -s foo bar
$ tree
.
├── bar -> foo
└── foo
    └── file1

2 directories, 1 file

OK, let's see the contents of bar:

$ ls bar
file1

What if we delete the file?

$ rm foo/file1
$ ls bar
$

The bar directory is now empty because it is just a symlink to foo so deleting foo/file1 also deleted bar/file1 because the two were the same file.

Conversely, deleting the link itself will not affect the original in any way because the link is just that, a link.

terdon
  • 100,812
  • I think it should be the other way round? You want a symlink in your home folder to the Dropbox location – MMM Apr 30 '14 at 11:48
  • @MMM no, you want to be able to edit files in ~/Documents and have those changes propagated to ~/Dropbox/Documents. If you do it the other way around you'd have to first delete the real Documents folder (you get a file exists error otherwise) or move it to ~/Dropbox and then link back again. This would also mean that if you ever deleted your Dropbox folder, you would loose all your Documents. – terdon Apr 30 '14 at 11:55
  • That's exactly what I was looking for, one last question, what happens if I delete the file in the link folder. i.e rm bar/file1 ? – Unbound Apr 30 '14 at 12:59
  • owh, figured it, that too delete the files, so I have to consider both the directories the same thing. change in one will affect the other. – Unbound Apr 30 '14 at 13:03
  • @Unbound exactly. Basically, there is only one dircetory, not 2. The link simply points back to the original. So, any files under the link are the exact same files as the original directory. Deleting the link itself is harmless, it just removed this pointer, deleting files that are under the link will delete the originals since those files are the originals, only the directory is a link. Oh, and if this solves your question, please remember to mark it as accepted by clicking on the tick mark under the vote count on the left. This will mark the issue as resolved. – terdon Apr 30 '14 at 13:06
  • @terdon: Interesting, I did not know that Dropbox will upload files/directories that are pointed at by a symlink. – MMM Apr 30 '14 at 16:55
0

Similar question: check this Automatically mirror a directory in Ubuntu

What you are trying to do is what RAID and other stuffs do, it would be great if you read about them maybe it will give you a better solution. Creating a backup for another folder in the same drive is not really good.

eakdev
  • 1
  • The OP is using Dropbox, the backup is created on Dropbox's servers. RAID is serious overkill and not really relevant here. – terdon Apr 30 '14 at 11:41
  • @terdon you're right it's a bit overkill but since based on his question he seems to want to have another backup aside from dropbox, I mentioned it. :) – eakdev Apr 30 '14 at 11:45
  • no, I want to create link to the dropbox folder so that I don't have to copy paste every time I want to share something in dropbox – Unbound Apr 30 '14 at 12:50
  • Then symlink should do :) – eakdev May 01 '14 at 04:19
0

I would suggest to use rysnc command :

rsync (stands for remote synchronization) is an open source tool for data transfers between Unix systems.

rsync synchronizes directories – makes one directory look (contain the same files and subdirectories) exactly like another one. by comparing them as per specified criteria (file size, creation/modification date or checksum) .

How to use rsync :

sudo rsync -az /pathof folder A /pathof folder B

sudo rsync -az /pathof folder B /pathof folder A

-a archive mode (implies recursive, copy symlinks as symlinks, preserve owner, modification times, group, owner, special and device files)

-z compresses the data

I would Suggest also to use watch command which will repeat rsync every interval of time , or use cron .

For more info about watch command : Here

Reference : Here

nux
  • 38,017
  • 35
  • 118
  • 131