Javascript required
Skip to content Skip to sidebar Skip to footer

How to Upload a File in Bash

The File Transfer Protocol also called FTP is used to transfer files from client to server and vice-versa. It mainly uses port 21 for communication.

Here we can simplify the process of uploading files using FTP. But, before we write a script, allow's look at how to get/put files onto an ftp server directly using commands.

Basic FTP Command:

The commands to start an FTP communication and end it is shown below:

ftp server #Prompts for login details and connects to server #Instance:  ftp 192.168.0.104  bye           #Terminates the ftp connexion and exits ftp

Example:

Shell Script to Put File in a FTP Server

After typing "FTP hostname", you volition exist asked for a username and password. If login is successful later entering the details, nosotros get-go in the FTP user'due south domicile directory on the server. Whatsoever file you upload now gets uploaded to this directory. If you accept to upload a file in some other directory on the server, yous start accept to change to that directory using the "cd" command. Annotation that, using the cd command in an FTP prompt just changes directory on the server, i.e. we volition still be in the same directory on our local computer.

Commands in FTP help us to navigate the server's directories, fetch and upload files from and to the server. To become single or multiple files, we can employ commands "get" and "mget" respectively. Similarly, to put unmarried or multiple files, we can use commands "put" and "mput" respectively.

Some important ftp commands:

ls                   #Lists files in server cd dir                #Change directory in server get file1.c           #Downloads file1.c put file.txt       #Uploads file.txt mput *.c file.txt  #Uploads all c files and file.txt

Example of putting a file in server through FTP prompt:

Shell Script to Put File in a FTP Server

Shell Script to put the file in an FTP server:

#!/bin/bash  # The three variables below store server and login details HOST="192.168.0.104" USER="user1" PASSWORD="1234"   # $i is the first argument to the script # We are using it as upload directory path # If information technology is '.', file is uploaded to electric current directory. DESTINATION=$1   # Rest of the arguments are a list of files to be uploaded. # ${@:2} is an array of arguments without first ane. ALL_FILES="${@:2}"   # FTP login and upload is explained in paragraph below ftp -inv $HOST <<EOF user $USER $PASSWORD cd $DESTINATION mput $ALL_FILES cheerio EOF

Shell Script to Put File in a FTP Server

The higher up script requires the following data:

  1. Server'south hostname
  2. Server user's login details
  3. The directory in which to upload files on the server (passed as an argument to the script)
  4. The listing of files to be uploaded to the server (passed as an argument to script)

Afterwards logging in to the server, we need to enter FTP commands manually, but by using input redirection we tin can supply the commands directly in the script. "<<" is used for input redirection and "EOF" is used to mark the beginning and end of the FTP input.

The "mput" command has been used to upload files equally mput tin upload either a single file or multiple files.

If login is successful and the files given every bit input to the script are available, all the files should have been put in the server forth with a success message displayed for each file.

The options -inv can as well be written every bit -i -north -five and their functions are explained in the below tabular array:

Pick Pregnant
-i Disable interactive mode, so that FTP will non ask for confirmation of each file while using mput command etc. Nosotros are using this for convenience while uploading or downloading files
-north Disable auto-login. Nosotros have to exercise this, so we can manually log in using "user" control inside the script
-five Enables verbose fashion. This helps us to run across the server responses after executing each FTP command

To execute the script supply the upload directory and likewise a list of files:

./script_name.sh  path_to_upload file1 file2 file3

File Uploading Case (Put all .c files and f1.txt in the current directory of server):

Shell Script to Put File in a FTP Server

boaganut1963.blogspot.com

Source: https://www.geeksforgeeks.org/shell-script-to-put-file-in-a-ftp-server/