Skip to content

Mac iterm2 配置rzsz命令

崧云悠揽月
April 8, 2025

rzsz介绍

rzsz 是一个用于在 Unix/Linux 系统中进行文件传输的工具,通常用于在终端中通过 ZMODEM 协议发送和接收文件。

步骤

1. 安装iterm2

官网下载即可。

2. 安装rzsz

shell
brew install lrzsz

安装后查看一下/usr/local/bin目录下是否有rzsz命令。如果没有,可以查看下rzsz的安装路径,并配置对应的软链

shell
brew list lrzsz

# Output::
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lrb
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lrx
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lrz
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lsb
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lsx
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lsz
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/rz
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/sz
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/sbom.spdx.json
#/opt/homebrew/Cellar/lrzsz/0.12.20_1/share/man/ (2 files)

ln -s /opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/rz /usr/local/bin/rz
ln -s /opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/sz /usr/local/bin/sz

3. 新建两个iterm2-文件

  1. iterm2-recv-zmodem.sh
shell
#!/bin/bash

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 --bufsize 4096
	sleep 1
	echo
	echo
	echo \# Sent \-\> $FILE
fi
  1. iterm2-send-zmodem.sh
shell
#!/bin/bash


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" --escape --binary --bufsize 4096
	sleep 1
	echo
	echo \# Received $FILE
fi

赋予他们执行权限

shell
sudo chmod +x /usr/local/bin/iterm2*

4. 配置iterm2

  • 打开iterm2,点击顶部菜单栏的iTerm2 -> Preferences -> Profiles -> Advanced -> Triggers -> Edit...
shell
Regular expression: rz waiting to receive.\*\*B0100
Action: Run Silent Coprocess
Parameters: /usr/local/bin/iterm2-send-zmodem.sh

Regular expression: \*\*B00000000000000
Action: Run Silent Coprocess
Parameters: /usr/local/bin/iterm2-recv-zmodem.sh

测试

shell
# 登录机器
ssh user@ip

# 发送文件
sz /path/to/file

# 接收文件
rz

最后更新时间: