One folder having two different locations on Ubuntu 18.04

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?


Solution 1:

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

Solution 2:

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.