Results 1 to 7 of 7

Thread: Shell script to transfer newly generated files by scp to remote server2

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Shell script to transfer newly generated files by scp to remote server2

    Hi all,
    I have root directory on server 1 say A and having sub directory B now my application generates output files and put in sub directory B.
    now i need to transfer these files from server1 to server2 by scp which is having same directory structure A and sub directory B
    I have tried a lot but seems need ur guidance.



    find /your/file/path/to/a -type -f -mtime 0 -print > output.txt
    find /your/file/path/to/b -type -f -mtime 0 -print >> output.txt
    find /your/file/path/to/c -type -f -mtime 0 -print >> output.txt
    awk '{print "scp "$0" user@hostname:/your/file/path/to/a /your/file/path/to/b /your/file/path/to/c"}' output.txt | sh

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,512
    Thanks
    17
    Thanked 799 Times in 511 Posts
    Rep Power
    10

    Default

    Can you paste the sample output.txt? If it is as follows:
    Code:
    /path/to/file1
    /path/to/file2
    /path/to/file3
    Something as follows should work:
    Code:
    #!/bin/bash
    _input="/tmp/file"
    _user="USERNAME"
    _server="server2.nixcraft.com"
    
    while IFS= read -r line
    do
        f="$line"
        d="${f%/*}"
        # if remote $d dir, does not exits, create it
        ssh ${_user}@${_server}:$d [ ! -d "$d" ] && mkdir -p "$d"
        # copy file
        scp "$f" ${_user}@${_server}:$d
    done <"$_input"
    A better alternative is to use the rsync command.
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    output is like

    /your/file/path/to/a/1.txt
    /your/file/path/to//b/2.txt
    /your/file/path/to/c/output3.txt
    your/file/path/to/c/1.txt
    /your/file/path/to/c/2.txt

    and I dont have rsync pkg installed on unix box

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    My script is like
    code:
    STAMP=$(perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime(time() - 100);
    printf "&#37;04d%02d%02d%02d%02d", $YY + 1900, $MM + 1, $DD, $hh, $mm')
    touch $STAMP /sasdata/copydata/
    find /sasdata/copydata -type f ! -newer /sasdata/copydata/ -print > output3.txt
    awk '{print "scp "$0" sasadmin@server2:/sasdata/copydata/a /sasdata/copydata/b "}' output3.txt | sh

    code:
    output3.txt output is :

    /sasdata/copydata/a/1.txt
    /sasdata/copydata/b/2.txt
    /sasdata/copydata/output3.txt

    scp: /sasdata/copydata/a: not a regular file
    cp: /sasdata/copydata/b/2.txt and /sasdata/copydata/b/2.txt are identical


    I need to copy files to their respective directories but it gives error
    Last edited by tushar_spatil; 14th October 2011 at 05:25 PM. Reason: formating

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    am i doing anything wrong here please suggest
    Last edited by tushar_spatil; 17th October 2011 at 04:49 PM.

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    scp: /sasdata/copydata/a: not a regular file
    cp: /sasdata/copydata/b/2.txt and /sasdata/copydata/b/2.txt are identical


    please suggest

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    yes my ouput is as below

    /path/to/file1
    /path/to/file2
    /path/to/file3

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Script to transfer files from Linux to Windows
    By hunter in forum Shell scripting
    Replies: 4
    Last Post: 29th June 2011, 04:36 PM
  2. Replies: 3
    Last Post: 1st February 2011, 09:10 PM
  3. Remote FTP incremental copy shell script
    By manojbirari in forum Shell scripting
    Replies: 3
    Last Post: 7th November 2010, 09:19 PM
  4. Shell script to receive log files from a remote server
    By ratikanta123 in forum Shell scripting
    Replies: 3
    Last Post: 12th December 2009, 05:06 AM
  5. Script to delete a set of files in Remote machine
    By ashok333 in forum Shell scripting
    Replies: 4
    Last Post: 9th May 2008, 07:24 PM

Tags for this Thread

Posting Permissions

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