使用rz-sz对服务器接收和发送文件

注意
本文最后更新于 2024-12-05,文中内容可能已过时。

之前一直使用scp命令和rsync命令在服务器和本地之间传输文件,现在了解到rz sz命令,可以使用图形化的方式来传输,似乎更舒服。不过缺点是无法操作文件夹。

  1. 在服务器上安装。

bash

sudo apt install -y lrzsz
  1. 在自己的mac上安装。

bash

brew install lrzsz
  1. 因为使用的是iterm2,所以需要进行特定的配置。
    1. 首先创建两个配置文件,并对其赋予可执行权限。
    2. 创建iterm2-send-zmodem.sh。内容来自这里

bash

## !/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
  1. 创建iterm2-recv-zmodem.sh。内容来自这里

bash

## !/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	   
  1. 赋予权限。chmod +x iterm2-*

  2. 设置item2的Triggers模式。点击 iTerm2 的设置界面 Perference -> Profiles -> Default -> Advanced -> Triggers 的 Edit 按钮。 image.png

    1. 点击 + 号,添加如下的参数。

bash

		 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
  1. 配置好后如下。 image.png
  1. 当你ssh到服务器上,想从服务器拉去文件到本地,对于服务器是发送文件,因此使用sz filename
  2. 如果想上传本地文件到服务器,对于服务器是接收文件,因此使用rz