ssh 文件传输:你应该掌握的几种命令行工具

本文作者分享了在Linux系统中使用scp、lrzsz和trzsz命令进行文件传输的历程,包括scp的基本用法、lrzsz在iterm2中的配置以及trzsz的出现带来的便利性,特别提到trzsz支持tmux和显示传输进度的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

image.png
这篇文章主要分享一下我使用过的 ssh 传输文件的进阶路程,从 scp -> lrzsz -> trzsz,希望能给你带来一些帮助~

scp

scp 命令可以用于在 linux 系统之间复制文件,具体的语法可以参考下图
image.png
其实使用起来也还比较方便, 比如下面的例子,share目录下有一个1.txt文件,如果要使用 scp 命令来传到服务器上的话,需要执行 scp 1.txt root@服务器ip:/home/share注意将@后的地址替换成你自己的服务器 ip,分号后的路径是你要上传到的服务器目录
image.png
下载文件也可以使用 scp 来操作,比如我们的服务器上有一个 test.js文件,我们想把它下载到本地,那么可以执行这行命令 scp root@服务器ip:/home/share/test.js . 就可以了
image.png
用了一段时间后发现,scp 存在一些不便利的地方,比如我现在正在服务器上查看文件,这时想要把日志文件下载到本机,就要再开一个 shell,然后复制文件目录,再执行 scp 命令,太麻烦了,不适合我~

lrzsz

第二个要介绍的是 lrzsz 命令,我使用的终端工具是 iterm2, 所以需要进行一些配置才能使用,具体的步骤如下
本地安装 lrzsz命令

brew install lrzsz

进入 /usr/local/bin目录下,新增下面两个文件
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
	/opt/homebrew/bin/sz "$FILE" --escape --binary --bufsize 4096
	sleep 1
	echo
	echo \# Received "$FILE"
fi

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"
	/opt/homebrew/bin/rz --rename --escape --binary --bufsize 4096 
	sleep 1
	echo
	echo
	echo \# Sent \-\> $FILE
fi

然后打开 iterm2 的配置,Profiles -> Defualt -> Advanced -> Triggers -> Edit -> 新增
第一条是用于下载文件的命令 sz

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

第二条是用于接受文件的命令 rz

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

结果如下图
image.png
操作完成后本地就配置好了,接下来再到服务器上去安装 lrzsz命令,我使用的是 centOS 服务器,可以执行 yum install lrzsz就好了
等执行完毕后,就可以在服务器上上传和下载文件了,比如上传文件,在服务器上输入 rz命令后,会自动弹出文件选择框,然后选择你要传输的文件就可以了
image.png
传输成功显示应该是这样的
image.png
在使用了一段时间后,我又发现了一些不爽的点,比如:上传、下载大文件时,没法看进度,而且感觉速度也比较慢,另外不支持 tmux 终端等。经过一番查找,发现了trzsz命令。

trzsz

项目地址:https://trzsz.github.io/。trzsz 是一个简单的文件传输工具,而且兼容 tmux,一看介绍,这不就是我想要的吗?果断安装尝试一下
本地安装 trzsz命令

brew install trzsz

服务器安装 trzsz命令,这里我使用的是 python 来安装

python3 -m pip install --upgrade trzsz

这样就好了,然后在使用 ssh 的时候在命令前加上 trzsz就可以了
比如:

trzsz ssh roo@服务器ip

进入服务器后,我们可以使用 trz命令来上传文件,执行后,同样会自动弹出文件选择框,然后选择你要上传的文件就可以了,这里我选择了一个 jdk 的大文件
image.png
然后就能看到上传进度了
image.png
下载文件也很简单,直接输入 tsz 文件名,然后选择本地要保存的目录就可以了。

总结

本篇文章记录了我使用 ssh 传输文件的命令行工具进阶过程,后续如果我发现更好用的工具也会同步更新,如果你对这方面感兴趣的话~可以点个关注哦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值