Find the answer to your Linux question:
Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Irithori
Hi All, Im just new to scripting and i want to create a script that will copy a newly created file from one folder to another folder. Then when i ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    2

    Script that automatically copy files from one folder to another


    Hi All,

    Im just new to scripting and i want to create a script that will copy a newly created file from one folder to another folder. Then when i create another file with thesame name, it will make a backup of the original file and copy it to another folder (backup folder).

    Here's what i got.

    #!/bin/bash

    # Look for the latest file and write it on a file called list1.txt
    find /root/test1 -type f -ctime -1 > /root/list1.txt;

    # Show the list of files under the test2 folder then write it on a file called list2.txt
    ls /root/test2 > /root/list2.txt;

    sleep 5

    # Read the file called list1.txt then write it on a variable
    for file1 in $(cat /root/list1.txt);
    do
    for file2 in $(cat /root/list2.txt); # Read the file called list2.txt then write it on a variable
    do
    if [ $file2 != $file1 ]
    then
    cp -pr $file1 /root/test2
    elif [ $file1 == $file2 ]
    then
    cp -pr $file2 /root/test3
    cp -pr $file1 /root/test2
    fi
    done
    done

    When i create a new file on the first folder, it doesnt copy it to the second folder.

    Does my script correct?

    Hope someone can help me.

    Thanks in advance!

  2. #2
    Linux Newbie
    Join Date
    Aug 2006
    Posts
    124
    Hi.

    Did you look at the rsync command, which seems to do what you want (as I understand, copy new files in one folder into another one) ?

    CaptainDangeax

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,356
    The even better approach is to use lsyncd.
    lsyncd getīs notified of filesystemchanges via inotify and can trigger actions based on that.
    You must always face the curtain with a bow.

  4. #4
    Just Joined!
    Join Date
    Nov 2006
    Posts
    2
    Quote Originally Posted by CaptainDangeax View Post
    Hi.

    Did you look at the rsync command, which seems to do what you want (as I understand, copy new files in one folder into another one) ?

    CaptainDangeax
    Hi CaptainDangeax,

    Yes, copy newly created files from one folder into another one. But before copying the file, it will check first the destination folder if the file already exist then if the file exists it will make a backup copy of that file to another folder (backup destination folder). If the file doesnt exist, it will just copy into the destination.

    Thanks!

  5. #5
    Linux Newbie
    Join Date
    Aug 2006
    Posts
    124
    Double rsync. Rsync to your "another folder", then rsync to the destination.

    Regards

    CaptainDangeax

  6. #6
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,356
    ..which can be done via rsync config options:
    Code:
    -b, --backup                make backups (see --suffix & --backup-dir)
         --backup-dir=DIR        make backups into hierarchy based in DIR
         --suffix=SUFFIX         backup suffix (default ~ w/o --backup-dir)
    CaptainDangeax likes this.
    You must always face the curtain with a bow.

Posting Permissions

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