11

I want to have the same folder at two different locations in my Ubuntu. If I update something in one, it should also get updated in the other location.

How can I make it happen?

2
  • 8
    do those 2 locations need to be physical (as in 2x taking up space) or can they be links?
    – Rinzwind
    Jun 23, 2019 at 13:51
  • 19
    A symbolic link is the simplest solution to your requirement and is not dependent on superuser privileges. man ln for more information.
    – doneal24
    Jun 23, 2019 at 22:17

3 Answers 3

44

As pointed out in the comments but not as a proper answer:

In many cases, a symbolic link is the easiest solution.

You can create them easily on the command line (using the ln command with -s parameter). You can create them easily using a GUI as well: Most file browsers (nautilus, ...) let you create a symbolic link using drag and drop (like for moving a file) while holding down a modifier key (CTRL+SHIFT).

Example for command line usage:

$ mkdir first_dir
$ ln -s ./first_dir ./second_dir
$ ls 
first_dir  second_dir

$ touch ./first_dir/test_1
$ touch ./second_dir/test_2

$ ls ./first_dir 
test_1  test_2

$ ls ./second_dir
test_1  test_2
0
18

Use bind mounts.

Suppose you have an existing directory /home/pandey/original and want to mirror it to /home/pandey/mirror so that everything you do in either of them is automatically done in the other one as well.

This doesn't require any syncing or copying between the two directories. A bind mount is just another view to the original directory and what happens in one also happens in the other.

  1. Create (as your user) the new directory /home/pandey/mirror:

    mkdir /home/pandey/mirror
    
  2. bind-mount the original directory to the newly created path. This requires root access:

    sudo mount --bind /home/pandey/original /home/pandey/mirror
    
  3. Enjoy.

To undo this, simply

sudo umount /home/pandey/mirror
rmdir /home/pandey/mirror

See also this question and its outstanding self-answer over on stackexchange about bind-mounts.

6
  • 3
    +1, For sure this way is faster than the way I proposed;
    – user833907
    Jun 23, 2019 at 16:42
  • 13
    Would'd cd /home/pandey; ln -s original mirror have the same effect and not require sudo rights?
    – doneal24
    Jun 23, 2019 at 22:18
  • 19
    Yeah I'm wondering why people are proposing these roundabout solutions while this sounds like a classic use case for a simple sym link. What's the advantage? As far as I can see none of the advantages listed in the referenced question seem to apply here.
    – Voo
    Jun 24, 2019 at 8:44
  • 5
    @doneal24 I agree: symbolic linking has the same effect from a practical view point and does not require fundamental system changes and administrator privileges.
    – vanadium
    Jun 24, 2019 at 15:59
  • 2
    The linked question on bind mounts has a nice list of possible use cases. Just needing another named path to a directory is not one of them.
    – Dubu
    Jun 25, 2019 at 13:12
0

You can do it by setting a crontab job:

  • Open your terminal and type crontab -e
  • Press i in order to activate the insert mode of your vim editor.

You can enter the command you want to be done there, so you can copy all files of the folder in another folder. For example, if you want to copy all files in /home/user/folder_name to /home/user/public you should insert this command in your crontab file:

rsync /home/user/folder_name -r /home/user/public

This will copy all files in the desired interval. If you want to copy all files at 21:30 every day your edit should be:

 30 21 * * *  rsync /home/user/folder_name -r /home/user/public

For more information about crontab look here or use man crontab.

2
  • I would probably use bind mounts as @PerlDuck, given is an easy and maintainable way to do it. I would not use cronjobs since this could lead to all sort of performance problems on your machine if you end up syncing a folder into multiple locations and the files in the first folder are very large or numerous. My company is using this approach to emulate Dropbox/Drive behavior and now that we reached a large number of files, some of them having GB's of size, the solution simply does not scale anymore.
    – nonameable
    Jun 24, 2019 at 8:48
  • 2
    This is two directories, not one.
    – oxr463
    Jun 24, 2019 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .