Convert a File Ina Folder to Zip and Upload Inftp
The File Transfer Protocol besides called FTP is used to transfer files from customer to server and vice-versa. Information technology mainly uses port 21 for communication.
Here nosotros tin can simplify the process of uploading files using FTP. But, before we write a script, let's await at how to go/put files onto an ftp server straight using commands.
Bones FTP Control:
The commands to commencement an FTP communication and stop information technology is shown beneath:
ftp server #Prompts for login details and connects to server #Example: ftp 192.168.0.104 good day #Terminates the ftp connection and exits ftp
Case:
After typing "FTP hostname", you volition exist asked for a username and password. If login is successful after entering the details, nosotros start in the FTP user's home directory on the server. Whatsoever file you lot upload now gets uploaded to this directory. If yous have to upload a file in some other directory on the server, yous offset have to modify to that directory using the "cd" command. Notation that, using the cd command in an FTP prompt simply changes directory on the server, i.eastward. we volition all the same exist in the same directory on our local computer.
Commands in FTP assistance us to navigate the server's directories, fetch and upload files from and to the server. To get unmarried or multiple files, we can use commands "get" and "mget" respectively. Similarly, to put single or multiple files, we tin utilise commands "put" and "mput" respectively.
Some important ftp commands:
ls #Lists files in server cd dir #Change directory in server become 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 the file in an FTP server:
#!/bin/bash # The 3 variables below store server and login details HOST="192.168.0.104" USER="user1" PASSWORD="1234" # $one is the start argument to the script # We are using information technology equally upload directory path # If it is '.', file is uploaded to current directory. DESTINATION=$1 # Residuum of the arguments are a list of files to be uploaded. # ${@:2} is an array of arguments without first 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 adieu EOF
The above script requires the following data:
- Server's hostname
- Server user'southward login details
- The directory in which to upload files on the server (passed as an statement to the script)
- The listing of files to be uploaded to the server (passed as an argument to script)
After logging in to the server, we need to enter FTP commands manually, merely by using input redirection we can supply the commands directly in the script. "<<" is used for input redirection and "EOF" is used to mark the beginning and finish of the FTP input.
The "mput" command has been used to upload files every bit mput can upload either a unmarried file or multiple files.
If login is successful and the files given equally input to the script are available, all the files should have been put in the server along with a success bulletin displayed for each file.
The options -inv tin can likewise be written equally -i -due north -5 and their functions are explained in the below table:
| Pick | Meaning |
|---|---|
| -i | Disable interactive mode, so that FTP will non enquire for confirmation of each file while using mput command etc. Nosotros are using this for convenience while uploading or downloading files |
| -northward | Disable auto-login. We have to exercise this, so nosotros can manually log in using "user" control inside the script |
| -5 | Enables verbose mode. This helps us to come across the server responses after executing each FTP command |
To execute the script supply the upload directory and also a list of files:
./script_name.sh path_to_upload file1 file2 file3
File Uploading Instance (Put all .c files and f1.txt in the current directory of server):
Source: https://www.geeksforgeeks.org/shell-script-to-put-file-in-a-ftp-server/
0 Response to "Convert a File Ina Folder to Zip and Upload Inftp"
Post a Comment