<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">
Go to top icon

How to transfer files on cross-platform using shell script?

Ranajit Musale Nov 26, 2012

file transfer protocol Windows network Shell Scripting Solaris network Technology cross-platform networks

Sometimes it can be a huge pain when needing to transfer files between cross-platforms because things don’t always go smoothly. First you have to set up the shares, then you need to make sure the permissions are correct, and one can’t forget the need to use a file transfer protocol that each of the operating systems support.

Here we will see an example of how to transfer files on cross-platform networks. We will see transfer of files from Solaris network to Windows network.

I have followed the below mentioned steps to achieve the same

  1. Create user in Solaris network e.g. ftpuser
  2. Create shell script file which contains set of commands to:
    1. Get files (those needed to transfer) from local server
    2. Connect to the destination server (where we need to transfer files) using FTP credentials.
    3. Execute actual transfer of files from source server to destination server.
  3. Let’s name this file as movefiles.sh

  4. Here is the skeleton of the shell script file. We will see working example of the script later on.

  5. #!/bin/bash


    #Connect to windows ftp server
    #Declare variables and assign them related values which will be used for remote
    #server connection


    HOST=> #This is the FTP server’s host or IP address.
    USER=> #This is the FTP user that has access to the server.
    PASS=> #This is the password for the FTP user.


    #Create connection to remote server
    ftp -inv $HOST user $USER $PASS
    binary


    #Now as you are in remote environment, mention destination folder name where you
    #want to transfer desired files


    cd />
    lcd />


    #Copy files from source to destination
    mput *.*
    bye
    EOF


    #Optional, if you want to delete source files, once they get copied to desired
    #destination network
    cd />
    rm *.*

  6. Here is the example of working shell script i.e. movefiles.sh

  7. #!/bin/bash


    # Connect to windows ftp server
    HOST=192.168.1.204
    USER=ftpuser
    PASS=test123


    ftp -inv $HOST user $USER $PASS
    binary
    # Destination folder on remote network
    cd /vrrupload


    # Source folder on local network
    lcd /export/home/ftpuser/files
    mput *.*
    bye


    EOF


    # Delete source files, once they get copied to desired destination network
    cd /export/home/ftpuser/files
    rm *.*

  8. Put this shell script into home folder of solaris user. In our case, the path will be like

  9. /home/ftpuser/movefiles.sh

  10. If we want to transfer files over cross-networks on regular intervals, we can set crontab for this.
  11. We can achieve it by using “crontab –e “ linux command and setting something like this:


    > > * * * sh /home/ftpuser/movefiles.sh > /home/ftpuser/files-out_`date +%Y-%m-%d_%H:%M%S`.log 2>&1

Similar Blog

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.