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

#!/bin/bash
# Shell script to copy all files recursively and upload them to 
# remote FTP server (copy local all directories/tree to remote ftp server)
#
# If you want to use this script in cron then make sure you have
# file pointed by $AUTHFILE (see below) and add lines to it:
# host ftp.mycorp.com
# user myftpuser
# pass mypassword
# 
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
FTP="/usr/bin/ncftpput"
CMD=""
AUTHFILE="/root/.myupload"
 
if [ -f $AUTHFILE ] ; then 
  # use the file for auth
  CMD="$FTP -m -R -f $AUTHFILE $myf $remotedir $localdir"
else
  echo "*** To terminate at any point hit [ CTRL + C ] ***"
  read -p "Enter ftpserver name : " myf
  read -p "Enter ftp username : " myu
  read -s -p "Enter ftp password : " myp
  echo ""
  read -p "Enter ftp remote directory [/] : " remotedir
  read -p "Enter local directory to upload path [.] : " localdir
  [ "$remotedir" == "" ] && remotedir="/" || :
  [ "$localdir" == "" ] && localdir="." || :
  CMD="$FTP -m -R -u $myu -p $myp $myf $remotedir $localdir"
fi
 
$CMD
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
18 comments… add one
  • Rahul Aug 7, 2012 @ 16:51

    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???

  • Astral May 2, 2012 @ 14:08

    Fantastic! Thanks !

  • andrew Apr 23, 2012 @ 5:11

    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

  • Pavan Nov 14, 2011 @ 6:32

    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 Dec 24, 2011 @ 10:46

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

  • Gaurab Aug 5, 2010 @ 5:42

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

  • Pelle May 3, 2010 @ 17:21

    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

  • Brendon Feb 13, 2010 @ 11:32

    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?

  • jamuna Dec 24, 2009 @ 7:20

    HI please explain it

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

  • Fred THL Dec 21, 2009 @ 12:22

    Why not considering rsync over ssh ?

  • BJones Dec 13, 2009 @ 20:30

    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.

  • Rotx Sep 10, 2009 @ 9:46

    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 .

  • Dabidi Jul 8, 2009 @ 9:16

    How to skip upload existing files on remote server?

  • me May 19, 2009 @ 12:12

    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

  • me May 19, 2009 @ 11:13

    I love you!

  • elejido Apr 22, 2009 @ 22:16

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

  • O2Link Apr 20, 2008 @ 11:56

    Thank you!
    it was so usefull for my work.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.