When backing up data in daily life, the time-saving method is to transfer data through ftp in the background. You can try the following operation methods.
Log in to FTP to achieve the function of downloading files
FTP server: 192.168.0.199 FTP path: /ftphome/data Local path: /local/dataThe script to download the file from FTP to the local area is as follows:
Download script code in batches
#Batch download files from FTP to local #!/bin/sh ftp -v -n 192.168.0.199<<EOF user ftpuser ftppwd binary cd /ftphome/downloadData lcd /local/getDownloadData prompt mget * bye EOF echo "download from ftp successfully"
Download single file script code
#Download single file from FTP to local #!/bin/sh ftp -v -n 192.168.0.199<<EOF user ftpuser ftppwd binary cd /ftphome/downloadData lcd /local/getDownLoadData prompt #get get bye EOF echo "download from ftp successfully"
Log in to FTP to achieve file upload function
FTP server: 192.168.0.199 FTP path: /ftphome/uploadData Local path: /local/getUploadData The script for passing the file from the local to FTP is as follows:
Upload script code in batches
#Batch upload documents from local to FTP #!/bin/sh ftp -v -n 192.168.0.199<<EOF user ftpuser ftppwd binary hash cd /ftphome/uploadData lcd /ftphome/getUploadData prompt mput * bye #here document EOF echo "commit to ftp successfully"
Upload single file script code
#Upload individual documents from local to FTP #!/bin/sh PUTFILE = ftp -v -n 192.168.0.199<<EOF user ftpuser ftppwd binary cd /ftphome/uploadData lcd /ftphome/getUploadData prompt put $PUTFILE bye #here document EOF echo "commit to ftp successfully"
Command explanation
Log in to FTP
ftp -i -n 192.168.0.199 << EOF
<< is input using instant file redirection EOF is the flag of instant file which must appear in pairs to identify the beginning and end of instant file. Several common signs of ftp are:
- -d: Use debug mode, but you must edit the /etc/ file and add one of the following: FileName or FileName.
- -g: Disable metacharacter extension in file names, that is, cancel the global file name.
- -i: Close interactive prompts in multi-file transfer.
- -n: Prevent automatic login in the start connection. Otherwise, the ftp command searches for the $HOME/.netrc login item, which describes the login and initialization process of the remote host.
- -v: Displays all responses of the remote server and provides statistical information for data transmission, that is, displays detailed processing information when the program is running.
Enter the FTP username and password
user ftpuser ftppwd
ftpuser: Username when logging in to FTP ftppwd: Password when logging in to FTP
Transfer files through binary command
binary
FTP file transfer types are: ascii, binary, ebcdic, image, local M and tenex.
- – ascii: Set file transfer type to network ASCII. This type is the default value, that is, the default is to use Ascii mode for transmission.
- – binary: Set the file transfer type to a binary image. The types of files that need to be transferred using binary include ISO files, executable files, compressed files, pictures, etc. This type may be more efficient than ASCII transmission.
- – ebcdic: Set the file transfer type to EBCDIC.
- – image: Set file transfer type to binary image. This type may be more efficient than ASCII transmission.
- – local M: Set the file transfer type to local. The M parameter defines the decimal number for each computer word bit. There is no default value for this parameter.
- – tenex: Set the file transfer type to the type required by the TENEX machine.
Toggle hash symbol (#) Print
hash
When a data block is transmitted using the get or put command, let FTP display a #, which is a visible signal to determine the data being transmitted, useful when the user is unsure whether the network is working. When a large file is transferred, if the FTP has displayed this information, it means that the transfer is in progress. The hash command is a Boolean variable command. Use the hash command to turn on the display# switch, and then use the hash command to turn off the display.
Switch directory
Enter the corresponding folders locally and FTP respectively: Go to the corresponding path on FTP (here means entering the folder /ftphome/downloadData):
cd /ftphome/downloadData
The corresponding path to the local area (here it is indicated in the folder of /ftphome/getDownloadData):
cd /ftphome/getDownloadData
Switch interactive prompts
prompt
When using mget or mput, the propt command lets FTP prompt before transferring each file, which prevents overwriting existing files. If the prompt has been started when the prompt is issued, FTP will turn off the prompt. At this time, there will be no questions asked if all files are transferred.
File transfer
Download the file
Download multiple files: Format: mget [remote-files] For example: Get all files in the remote folder
mget * #or mget *.*
Note: mget. Every time a file is downloaded, there will be a prompt. If you want to remove the prompt, execute:prompt off before the mget . command.
Download a single file: Format: get [remote-file] [local-file] For example: Get files on remote FTP
get
Upload file
Upload multiple files: Format: mput local-files For example: Upload all files in the folder to FTP
mput *
Upload a single file: Format: put local-file [remote-file] For example: Upload local files to remote FTP
put
Disconnect
bye
End the file transfer session and exit the ftp command, the same as the quit command.
Divider flag
... << EOF #execute shell EOF
EOF is just a delimiter flag, which can be replaced by abc, !, etc., and the same function is the same, but everyone is used to using EOF to represent it. <<Usage: When the shell sees <<, it will know that the next word is a delimiter. The content after the delimiter is taken as input until the shell sees the delimiter again (on a separate line). Therefore, the delimiter can be any string defined.
Solution: The ftp command line does not support directory downloads and is solved through wget#wget ftp://IP:PORT/* --ftp-user=xxx --ftp-password=xxx -rNote: Asterisk* must be available, otherwise the downloaded file is only one file -r parameter is used for directory download
This is the article about Shell scripts automatically uploading and downloading files. For more relevant Shell FTP automatic uploading and downloading files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!