Flaws of the ftp command
In FTP, put
commands are usually used to upload individual files, not entire folders. If you want to upload entire folders, you can use ncftp
command line tools or other FTP clients that support recursive uploads.
Here is ncftp
an example of recursively uploading folders using the command line tool:
#!/bin/bash
# 设置变量
FTP_SERVER="192.xx.xx.xx"
FTP_USER="user-xxx"
FTP_PASS="passwd-xxx"
LOCAL_DIR="/path/to/local/folder"
REMOTE_DIR="/path/on/remote/server/"
# 使用ncftp命令上传文件夹
ncftp -u $FTP_USER -p $FTP_PASS $FTP_SERVER << EOF
cd $REMOTE_DIR
mkdir $(basename "$LOCAL_DIR")
cd $(basename "$LOCAL_DIR")
lcd $LOCAL_DIR
put -R ./*
bye
EOF
Please /path/to/local/folder
replace with the path of the actual local folder to be uploaded, and replace /path/on/remote/server/
with the path of the remote directory of the folder to be stored on the FTP server.
In the script, we use ncftp
commands to connect to the FTP server and provide a series of commands in input redirection. -u
and -p
options are used to provide FTP username and password for authentication. We then executed the following FTP commands in sequence:
cd
: Switch to the remote directory.mkdir
: Create a folder on the remote server with the same name as the local folder.cd
: Switch to the newly created remote folder.lcd
: Switch to the local folder.put -R
: Recursively upload the local folder to the remote server.
By using an FTP client that supports recursive uploads, for example ncftp
, you can upload an entire folder to a specified FTP server in a shell script.
install ncftp
yum install epel-release -y
yum install ncftp -y
ncftp upload multiple directories at once
In the FTP protocol, put -R
commands are used to recursively upload the contents of a directory and its subdirectories to a remote server. It does not support uploading multiple directories at once.
If you want to upload multiple directories at once, you can write a loop or use other script logic to do it. Here is an example script that demonstrates how to upload multiple directories at once:
#!/bin/bash
# 设置变量
FTP_SERVER="192.xx.xx.xx"
FTP_USER="user-xxx"
FTP_PASS="passwd-xxx"
REMOTE_DIR="/path/on/remote/server/"
# 定义本地目录列表
LOCAL_DIRS=(
"/path/to/local/dir1"
"/path/to/local/dir2"
"/path/to/local/dir3"
)
# 使用ncftp命令上传多个目录
for dir in "${LOCAL_DIRS[@]}"; do
ncftpput -R -u $FTP_USER -p $FTP_PASS $FTP_SERVER $REMOTE_DIR "$dir"
done
In the script, we first define an LOCAL_DIRS
array containing multiple local directory paths to upload. Then, use for
a loop to iterate through the array, ncftpput
uploading each directory one by one to the remote server using commands.
Make sure to replace /path/to/local/dir1
, /path/to/local/dir2
etc. with the actual local directory path to be uploaded and will be /path/on/remote/server/
replaced with the remote path of the directory to be stored on the FTP server.
In this way, you can upload multiple directories to a specified FTP server at one time in a shell script.