介绍
远程文件传输的工具有很多,例如sz/rz、scp、sftp、ftp等。Mac中的命令终端不支持使用rz和sz命令进行文件上传和下载,可以通过安装终端iTerm2并扩展安装lrzsz实现使用sz和rz命令进行文件传输。
sz (send Zmodem) / rz (receive Zmodem)可作为一个在MacOS环境下结合iTerm2使用的一个文件上传下载工具(命令)。
安装
1. 安装iTerm2
建议去官网下载。
2. 安装sz/rz
2.1 brew install lrzsz
如果提示没有brew,需要先安装brew(见参考资料[1])。
2.2 配置sz/rz.sh文件
cd /usr/local/bin
注:其他路径亦可。
在目录下创建两个文件:
vi iterm2-recv-zmodem.sh
vi iterm2-send-zmodem.sh
其中iterm2-recv-zmodem.sh 内容如下:
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain
osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi
if [[ $FILE = "" ]]; then
echo Cancelled.
# Send ZModem cancel
echo -e \\x18\\x18\\x18\\x18\\x18
sleep 1
echo
echo \# Cancelled transfer
else
cd "$FILE"
/usr/local/bin/rz -E -e -b
sleep 1
echo
echo
echo \# Sent \-\> $FILE
fi
其中iterm2-send-zmodem.sh 内容如下:
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain
osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi
if [[ $FILE = "" ]]; then
echo Cancelled.
# Send ZModem cancel
echo -e \\x18\\x18\\x18\\x18\\x18
sleep 1
echo
echo \# Cancelled transfer
else
/usr/local/bin/sz "$FILE" -e -b
sleep 1
echo
echo \# Received $FILE
fi
文件创建完成后:wq
保存,给文件添加权限
chmod 777 iterm2-*
3. 配置iTerm2
配置好.sh文件之后,接下来对iTerm2进行配置以添加sz/rz功能。
点击 iTerm2 的设置界面 Perference->Profiles->Default->Advanced->Triggers
的 Edit 按钮,
Regular expression: rz waiting to receive.\*\*B0100
Action: Run Silent Coprocess
Parameters: /usr/local/bin/iterm2-send-zmodem.sh
Instant: checked
Regular expression: \*\*B00000000000000
Action: Run Silent Coprocess
Parameters: /usr/local/bin/iterm2-recv-zmodem.sh
Instant: checked
使用sz/rz命令
-
sz
进入到一个目录下,sz 文件名称
,可以发送(下载)一个文件到本机,最后在弹窗中选择下载文件需要保存的目录。
-
rz
进入到一个目录下,rz
,在弹窗中选择需要上传的文件。
注意事项
- sz/rz只适合小文件传输,大文件还是要使用Filezilla与xftp等工具进行传输,否则可能出现报错(例如“满屏打印”)。
- 只能传输文件,不能传输文件夹。
- 不是所有工具都支持rz与sz,必须支持ZModem协议才行,如putty就不能使用rz与sz。
参考资料
[1] mac下镜像飞速安装Homebrew教程
[2] 超级好用的文件传输命令rz与sz
[3] rz sz
[4] Mac iTerm2,使用rz和sz无效,解决方式
[5] Mac osx 下安装iTerm2,并使用rz sz上传下载(附homebrew配置)
[6] Mac osx 下安装iTerm2,并使用rz sz上传下载(附homebrew配置) - SegmentFault 思否
[7] iterm2.com