Shell script to copy all files recursively and upload them to remote FTP server

by on April 9, 2008 · 18 comments

  1. #!/bin/bash
  2. # Shell script to copy all files recursively and upload them to
  3. # remote FTP server (copy local all directories/tree to remote ftp server)
  4. #
  5. # If you want to use this script in cron then make sure you have
  6. # file pointed by $AUTHFILE (see below) and add lines to it:
  7. # host ftp.mycorp.com
  8. # user myftpuser
  9. # pass mypassword
  10. #
  11. # This is a free shell script under GNU GPL version 2.0 or above
  12. # Copyright (C) 2005 nixCraft
  13. # Feedback/comment/suggestions : http://cyberciti.biz/fb/
  14. # -------------------------------------------------------------------------
  15. # This script is part of nixCraft shell script collection (NSSC)
  16. # Visit http://bash.cyberciti.biz/ for more information.
  17. # -------------------------------------------------------------------------
  18.  
  19. FTP="/usr/bin/ncftpput"
  20. CMD=""
  21. AUTHFILE="/root/.myupload"
  22.  
  23. if [ -f $AUTHFILE ] ; then
  24. # use the file for auth
  25. CMD="$FTP -m -R -f $AUTHFILE $myf $remotedir $localdir"
  26. else
  27. echo "*** To terminate at any point hit [ CTRL + C ] ***"
  28. read -p "Enter ftpserver name : " myf
  29. read -p "Enter ftp username : " myu
  30. read -s -p "Enter ftp password : " myp
  31. echo ""
  32. read -p "Enter ftp remote directory [/] : " remotedir
  33. read -p "Enter local directory to upload path [.] : " localdir
  34. [ "$remotedir" == "" ] && remotedir="/" || :
  35. [ "$localdir" == "" ] && localdir="." || :
  36. CMD="$FTP -m -R -u $myu -p $myp $myf $remotedir $localdir"
  37. fi
  38.  
  39. $CMD


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • O2Link

    Thank you!
    it was so usefull for my work.

  • elejido

    If the SAME file exists in the remote server, the script overwrite it? append? resume?

  • http://www.cyberciti.biz/ Vivek Gite

    It will overwrite the file.

  • me

    I love you!

  • me

    If using the AUTHFILE option, don’t forget to set the following variables in this script

    myf=”my-ftp-server.com”
    remotedir=”/my/remote/dir”
    localdir=”/my/local/dir”

    And yes, you will need ncftp installed, if it is not already
    For OS X with mac ports,
    sudo port install ncftp

  • Dabidi

    How to skip upload existing files on remote server?

  • Rotx

    I am taking server name from a file and everything working fine buy my problem is if a server in the list is not connecting it is not going to next available server. How to get the ftp status in the script if ftp is not connecting how to skip to next available server .

  • BJones

    Can we add this script to cron and have it auto backup? I am looking to back up entire dir every week, like at 3am, when there is the least amount of traffic, but I don’t want to have to be around for this to happen.

  • Fred THL

    Why not considering rsync over ssh ?

  • jamuna

    HI please explain it

    [ “$remotedir” == “” ] && remotedir=”/” || :
    [ “$localdir” == “” ] && localdir=”.” || :

  • Brendon

    How can I make this GET the remote files. Obviously I used it to send them all there – now how can I send my 100GB back to the server?

  • Pelle

    Hello,
    I hade to delete “$myf” in the line below:
    CMD=”$FTP -m -R -f $AUTHFILE $myf $remotedir $localdir”

    Then it all went well.

    Thank you for an good and simple script!
    /Pelle

  • Gaurab

    Can all the files zipped in a single zip file before copying to other remoter server.

  • Pavan

    hi , I have shell script to sftp the files from one server to other server. When executed manually it works fine but when scheduled the script using crontab it fails.
    Below is the piece of code. Please help me in solving this issue. Thanks in advance.

     #!/bin/bash
     USER=username
     PASSWORD=password
     HOST=hostname
     dt=`date +%Y%m%d`
     fldt=`date --date "$dt 1 days ago" +%Y%m%d`
     ldir=/home/localdir
    sudo -u xyz sftp $USER@$HOST <<EOF
    $PASSOWRD
     cd rmtdir
     mget filename_$fldt*.dat.txt $ldir
     quit
     EOF
     if [ $? -ne 0 ]; then
     echo "sftp failed" | mailx -s "`date` :Sftp failed" pavan@ymail.com
     else
     echo "sftp success"| mailx -s "`date` :Sftp success" pavan@ymail.com
     fi
     exit 0
    
  • Goksel

    /usr/bin/sudo -u xyz /usr/bin/sftp

  • andrew

    Is there a way to skip sir, it’s very important as I’m doing massive amount of files, and our destination server’s connection isn’t reliable. Cheers

  • Astral

    Fantastic! Thanks !

  • Rahul

    Hi All,
    We are using commaand as,

    /usr/bin/ncftpput -u $username -p $passwd

    But it does not overwrite file on remote server if it already present.
    Any suggestions???

Previous Script:

Next Script: