Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a network share on my system to which our plotter scanner sends its scanned files. How can I write a script to automatically copy newly added files to another folder?

share|improve this question

2 Answers 2

cron and rsync would be my tools of choice. I'm going to assume you've mounted this network share through naultilus or another method. There are other, better ways to mount things than through Naultilus (especially for scripting purposes) but I'll leave that up to you:

Open a terminal and run crontab -e. You might be asked to select an editor (nano is easiest). In there you want to add a new line that looks something like this:

*/5 * * * * rsync -avz ~/.gvfs/remote/path/ ~/scans/

Control-x, then y to save.

That will sync everything down from ~/.gvfs/remote/path/ to ~/scans/ every five minutes. It won't re-download things that haven't changed. Of course you'll want to edit the paths and you might want to change the timing for the cron statement. You might want to limit it to in-hours, or run it more often.

share|improve this answer
  • Let $share contain the path to your "share (...)to which our plotter scanner sends its scanned files"
  • Let $afolder contain the path to your "another folder"

My simplisctic approach would be to keep a list of files in $share. Write a script that checks if the files currently in $share are in this list. It not, they must be new and should be moved. Then add this script to cron thus running it regularly.

Let $filelist contain the path to said filelist.

Script (not tested - just a template):

#!/bin/bash

...variables as discussed....
filelist=/some/path/to/the/aforementioned/filelist

find "$share" -type f >> /tmp/foundfiles
while read fnd; do  #walk files in folder
    isinlist=""
    while read listitem; do  #walk listitems
        if [ "$fnd" -ef "$listitem" ]; then
            echo "$fnd Allready in list."
            isinlist="inlist"
        fi
    done < "$filelist"
    if [ ! -n "$isinlist ]; then
        echo "$fnd" >> "$filelist"
        cp "$fnd" to "$afolder"
    fi
done < /tmp/foundfiles
rm /tmp/foundfiles
exit 0

There are tools that do that for you, like rsync. But maybe you want to add some custom-funtionality.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.