Linux – Copy new files from a monitored folder to another in debian

debianfile-transferlinux

I have directory a and directory b. Directory a has new files and folders copied to it periodically. I would like to monitor folder a for those new files and automatically copy them to folder b. Unfortunately due to some organizational scripts that I have set up previously in directory b preclude me from using rsync for these purposes since the folder structure at the destination would most likely differ too greatly between rsync runs.

Is there any kind of alternative setup that I can use?

Best Answer

Another way of doing this would be to use inotify:

  1. Install the inotify-tools package

     sudo apt-get install inotify-tools
    
  2. Write a little script that uses inotifywatch to check your folder for changes and moves any new files to the target directory:

     #!/usr/bin/env bash
    
     ## The target and source can contain spaces as 
     ## long as they are quoted. 
     target="/path/to/target dir"
     source="/path to/source/dir";
    
     while true; do 
    
       ## Watch for new files, the grep will return true if a file has
       ## been copied, modified or created.
       inotifywatch -e modify -e create -e moved_to -t 1 "$source" 2>/dev/null |
          grep total && 
    
       ## The -u option to cp causes it to only copy files 
       ## that are newer in $source than in $target. Any files
       ## not present in $target will be copied.
       cp -vu "$source"/* "$target"/
     done
    
  3. Save that script in your $PATH and make it executable, e.g.:

     chmod 744 /usr/bin/watch_dir.sh
    
  4. Have it run every time your machine reboots, create a crontab (crontab -e, as described in @MariusMatutiae's answer) and add this line to it:

     @reboot /usr/bin/watch_dir.sh 
    

Now, every time you reboot, the directory will automatically be watched and new files copied from source to target.

Related Question