Question: I would like to execute ftp from inside a shell script. I would also like to provide the username and password required for the FTP file download inside the shell script itself. Can you explain me how to perform this bath ftp from a shell script?
Answer: For downloading / uploading files using ftp in batch mode, you can either user FTP scripting using HERE document from command line, or call FTP from a shell script as explained below.
FTP Scripting using HERE document
$ ftp -in URL << SCRIPTEND user USERNAME PASSWORD binary cd PATH mget PATH SCRIPTEND
In the above ftp scripting:
- ftp : ftp command
- -i : Disable interactive prompting
- -n : Disable autologin
- user USERNAME PASSWORD : login using the supplied user name and password
- binary : Set binary mode for binary file transfer
- cd PATH : change directory in remote machine
- mget PATH : get the specified file
- SCRIPTEND : Both the tag represents HERE document start and end.
- HERE docs are used to provide input to commands, refer Here document to know more about it.
FTP Scripting using Shell Script
Following is a sample shell script script that downloads a tar file from the FTP server.
$ cat download.sh ftp -in mirrors.issp.co.th << SCRIPTEND user anonymous email@example.com binary cd cpan/authors/id/P/PM/PMQS/ mget Compress-Raw-Bzip2-2.027.tar.gz SCRIPTEND
Execute this shell script to download the files from the ftp.
$ sh download.sh $ ls download.sh Compress-Raw-Bzip2-2.027.tar.gz
For more FTP command refer our earlier article FTP and SFTP Beginners Guide with 10 Examples
Get the Linux Sysadmin Course Now!