How to sync two folders on my machine, in one directional syncing?

Solution 1:

Warning: this is for bidirectional syncing, but given that you are ok in doing things manually... I do not know if there is an option for unidirectional behavior in unison --- if anyone knows please feel free to edit this answer.

Install unison:

 sudo apt-get install unison unison-gtk

Create this file in the directory $HOME/.unison, call it test.prf (or whatever)

label = sync folders A and B
root = /home/romano/tmp/folderA
root = /home/romano/tmp/folderB

Run unison from the dash, choose your profile above and do a first synchronization. You'll have a graphic interface to choose what to copy and where.

After that, do changes and when you want to keep in sync, use unison again. It will let you decide what to do:

enter image description here

Solution 2:

Another (python) option, offering a bit of a "fancy" report:

----------------------------------------------------------
Newly copied from /home/jacob/map 1 to /home/jacob/map 2:
- Naamloos document
- pscript_2.py
- test123
- monkey_out.txt
----------------------------------------------------------
Unique files in /home/jacob/map 2:
- file_in_targetfolder
----------------------------------------------------------

It does what you describe:

  • it copies files from folder A to B if they do not (yet) exist in B
  • reports files in B, which are not in A

The script

#!/usr/bin/env python3
import sys
import os
import shutil

folder_1 = sys.argv[1]; folder_2 = sys.argv[2]
files_1 = os.listdir(folder_1); files_2 = os.listdir(folder_2)
# make the length (n) of separating line "smart"
s = "\nNewly copied from "+folder_1+" to "+folder_2+':'; n = len(s)
print("\n"+n*"-"+s)
for f in files_1:
    f1 = folder_1+"/"+f
    if all([os.path.isfile(f1), not f in files_2]):
        shutil.copyfile(f1, folder_2+"/"+f)
        print("-",f)
print(n*"-"+"\nUnique files in "+folder_2+":")
for f in files_2:
    f2 = folder_2+"/"+f
    if all([os.path.isfile(f2), not f in files_1]):
        print("-",f)
print(n*"-")

How to set up

  1. Copy the script into an empty file, save it as sync_report.py

  2. Test- run it by the command:

    python3 /path/to/sync_report.py <folder_a> <folder_b>
    

    If the directories contain spaces, put them between single quotes.

  3. If all works fine, use it as above, or:

    • make the script executable
    • add the following command to a shortcut key:

      gnome-terminal -e "'/path/to/sync_report.py' '/path/to/folder_a' '/path/to/folder_b'"
      

      Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command.

    Then, when the key combination is pressed:

    enter image description here

    Don't let the background distract you, my terminal is semi-transparent. :)

Alternatively, as mentioned by @ParanoidPanda (thanks for mentioning), add it to your ~/.bashrc file. Create an alias which calls the script:

alias <nameofalias>='python3 /path/to/sync_report.py <folder_a> <folder_b>'