Mac在iTerm2下使用sz/rz上传下载文件

介绍

远程文件传输的工具有很多,例如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命令

  1. sz
    进入到一个目录下,sz 文件名称,可以发送(下载)一个文件到本机,最后在弹窗中选择下载文件需要保存的目录。
    在这里插入图片描述

  2. rz
    进入到一个目录下,rz,在弹窗中选择需要上传的文件。
    在这里插入图片描述

注意事项

  1. sz/rz只适合小文件传输,大文件还是要使用Filezilla与xftp等工具进行传输,否则可能出现报错(例如“满屏打印”)。
  2. 只能传输文件,不能传输文件夹。
  3. 不是所有工具都支持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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TracelessLe

❀点个赞加个关注再走吧❀

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值