0. 为什么要使用rz sz上传下载
开发的时候会用自己的Mac连接远程的机器进行开发,在Mac和远程机器之后需要进行文件的传输。
1. 安装iTerm2
建议去官网下载安装:http://www.iterm2.com/
2. 安装Homebrew
先到官网按照提示安装:https://brew.sh/
将框中的命令复制下来,然后在输入iTerm中,会车。
期间按照提示输入密码或者打回车,我在安装的时候出现了好几次time out的错误,然后多试了几次就装好了。
装好之后在命令行输入brew,如果出现如下的信息就说明装好了。
3. 安装lrzsz
在iTerm2中用命令
brew install lrzsz
进行安装,安装好之后用brew list lrzsz
命令查看lrzsz安装的位置:
下面的两个文件需要根据rz和sz的实际安装位置来写。
然后进入cd /usr/local/bin
里面,进行配置,首先新建两个文件,iterm2-recv-zmodem.sh和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"
# 这里是根据sz的安装位置来设定的
/opt/homebrew/Cellar/lrzsz/0.12.20_1/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
# 这里是根据sz的安装位置来设定的
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/sz "$FILE" -e -b
sleep 1
echo
echo \# Received $FILE
fi
注意:
将文件写好后保存好,使用如下命令添加权限:
chmod +777 iterm2-*
在往文件里写配置的时候可能会出现不让写的情况,这时我们可以先用:
sudo touch iterm2-recv-zmodem.sh
sudo touch iterm2-send-zmodem.sh
来创建两个文件,然后用:
chmod +777 iterm2-recv-zmodem.sh
chmod +777 iterm2-send-zmodem.sh
来使两个文件变成可读可写可执行的,然后在往里面写入配置。
4. iTerm2 配置添加rz sz 功能
(1)点击 iTerm2 的设置界面 Perference-> Profiles -> Default -> Advanced -> Triggers 的 Edit 按钮
(2)
(3)
在上图所示的框里面填入下面所示的信息:
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
5. 使用上传下载功能
rz 上传功能:
在bash中,也就是iTerm2终端输入rz 就会弹出文件选择框,选择文件 choose 就开始上传,会上传到当前目录
sz 下载功能:
sz fileName(你要下载的文件的名字) 回车,会弹出窗体 我们选择要保存的地方即可。