4

I have a set-up in which I create a certain file, and subsequently move it into a storage area for that type of file (folder A). There is another folder on my machine where I must also copy the file to (folder B).

Is there any way to make a set-up in which:

  • a file, created in folder A, gets automatically copied to Folder B, if it doesn't exist in folder B?
  • I will be notified if new file(s) appear in Folder B, which are not in folder A?

Both folders are on the same file system.

It doesn't have to be fully automatic; I would prefer to run some command in terminal to check for new files in need to be copied to Folder B, copy them and notify me which files have been copied. Also if there are any files in Folder B that are not in Folder A.

I am running Ubuntu GNOME 15.04, with GNOME Shell, and GNOME 3.16.

Jacob Vlijm
  • 85,475

4 Answers4

4

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

Rmano
  • 32,167
1

This should get you going. zenity has many more applications beyond the scope of your question; customize this as you like.

#!/bin/bash
ls -1 ./FolderA | sort > FolderA.txt
ls -1 ./FolderB | sort > FolderB.txt
FILESTOCOPY=$(diff FolderA.txt FolderB.txt | grep \< | awk '{print $2;}' | zenity --list --title="Select files to be copied" --co$
echo $FILESTOCOPY
NEWFILES=$(diff FolderA.txt FolderB.txt | grep \> | awk '{print $2;}' | zenity --list --title="New files in Folder B" --column="f$
echo $NEWFILES

Some explanation:

The ls -1 commands list the file names one line at a time, but only the names, no other information. This script will therefore not notice when a file in one folder is newer than one in the other folder with the same name.

Grepping for "<" will give us files in Folder A but not in Folder B, and vice versa for ">". You could use the variables created by zenity to build a copy command, or something else.

Jos
  • 30,529
  • 8
  • 89
  • 96
1

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>'
Jacob Vlijm
  • 85,475
0

Have a look at https://github.com/Fitus/Zaloha.sh. It a shell script that runs out of the box. It is interactive, so you will see and confirm what it is going to do.

Petas
  • 1