Upload Data to Ftp Server Command Line Linux

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 nosotros tin simplify the process of uploading files using FTP. But, before nosotros write a script, let'south look at how to get/put files onto an ftp server directly using commands.

Basic FTP Command:

The commands to showtime an FTP communication and end it is shown beneath:

ftp server #Prompts for login details and connects to server #Example:  ftp 192.168.0.104  cheerio           #Terminates the ftp connection and exits ftp

Example:

Shell Script to Put File in a FTP Server

After typing "FTP hostname", you will be asked for a username and password. If login is successful after inbound the details, we offset in the FTP user'due south home directory on the server. Whatever file you upload now gets uploaded to this directory. If you have to upload a file in some other directory on the server, y'all first have to change to that directory using the "cd" command. Note that, using the cd command in an FTP prompt only changes directory on the server, i.due east. we will withal 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 go single or multiple files, we can use commands "get" and "mget" respectively. Similarly, to put single or multiple files, nosotros can utilise commands "put" and "mput" respectively.

Some important ftp commands:

ls                   #Lists files in server cd dir                #Change directory in server go 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

Beat Script to put the file in an FTP server:

#!/bin/bash  # The 3 variables beneath store server and login details HOST="192.168.0.104" USER="user1" PASSWORD="1234"   # $1 is the first argument to the script # We are using it as upload directory path # If it is '.', file is uploaded to current directory. DESTINATION=$1   # Balance of the arguments are a list of files to exist uploaded. # ${@:2} is an array of arguments without showtime one. 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 above script requires the following information:

  1. Server's hostname
  2. Server user'southward login details
  3. The directory in which to upload files on the server (passed every bit an statement to the script)
  4. The list of files to exist uploaded to the server (passed as an statement to script)

After logging in to the server, we need to enter FTP commands manually, just by using input redirection nosotros can supply the commands directly in the script. "<<" is used for input redirection and "EOF" is used to mark the beginning and stop of the FTP input.

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

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

The options -inv can besides exist written equally -i -n -v and their functions are explained in the below table:

Pick Pregnant
-i Disable interactive mode, so that FTP will not ask for confirmation of each file while using mput command etc. We are using this for convenience while uploading or downloading files
-n Disable car-login. Nosotros have to do this, so nosotros can manually log in using "user" command within the script
-v Enables verbose style. This helps us to see the server responses afterwards executing each FTP command

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

./script_name.sh  path_to_upload file1 file2 file3

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

Shell Script to Put File in a FTP Server

gowanbabream.blogspot.com

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

0 Response to "Upload Data to Ftp Server Command Line Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel