Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, Trying to make a pretty basic bash script. The first thing that I need to do is compare two folders and copy any files that don't exists in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru jmadero's Avatar
    Join Date
    Jul 2007
    Location
    California
    Posts
    1,998

    Bash Script to Compare Two Folders and Copy Non Existant Files


    Hi All,

    Trying to make a pretty basic bash script. The first thing that I need to do is compare two folders and copy any files that don't exists in folder2 but do exist in folder1 to folder2.

    Example:
    Folder1 Contains: 12.txt, abc.txt, 2bad.txt
    Folder 2 Contains: 12.txt,2bad.txt

    I need the script to compare the two folders and copy/paste abc.txt into Folder 2.

    Notes:
    1. I'll need some kind of a loop because I add files to Folder1 routinely and the point is to create a cron job which will continue to populate Folder2.

    Thanks!
    Bodhi 1.3 & Bodhi 1.4 using E17
    Dell Studio 17, Intel Graphics card, 4 gigs of RAM, E17

    "The beauty in life can only be found by moving past the materialism which defines human nature and into the higher realm of thought and knowledge"

  2. #2
    Linux Guru bigtomrodney's Avatar
    Join Date
    Nov 2004
    Location
    Ireland
    Posts
    6,133
    While there are certainly ways to do this in the shell, or essentially any language you like...is there a reason you want to avoid using rsync?

    I personally think it's one of the greatest tools in existence and is pretty much the master of this kind of task.

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,356
    rsync works.
    You could additionally use lsyncd, which is based on inotify.
    So the files would be rsynced as soon as they are close-written.

    Another approach would be to use git.
    Create two git repos and a cronjob that pulls from the repo in folder 1 to the one in folder 2.

    Git would have the benefits of revisioned files (aka rollbacks are easily possible), history and branches (for .e.g. testing in folder 1 without updating folder 2)
    You must always face the curtain with a bow.

  4. #4
    Linux Guru jmadero's Avatar
    Join Date
    Jul 2007
    Location
    California
    Posts
    1,998
    I actually didn't think rsync would work because I will be modifying folder 2 but I learned that there is an --ignore-existing option which will do the trick Thanks!
    Bodhi 1.3 & Bodhi 1.4 using E17
    Dell Studio 17, Intel Graphics card, 4 gigs of RAM, E17

    "The beauty in life can only be found by moving past the materialism which defines human nature and into the higher realm of thought and knowledge"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •