掌握transfer.sh文件传输服务的实用技巧
transfer.sh是一个简单高效的文件传输服务,允许用户通过命令行快速上传和下载文件。本文将详细介绍transfer.sh的各种实用技巧,帮助您充分利用这个工具。
基础配置:创建传输别名
为了简化使用流程,我们可以为常用shell创建别名。
Bash/Zsh用户配置
对于Bash或Zsh用户,可以将以下函数添加到.bashrc
或.zshrc
文件中:
使用curl版本:
transfer() {
curl --progress-bar --upload-file "$1" https://transfer.sh/$(basename "$1") | tee /dev/null;
echo
}
alias transfer=transfer
使用wget版本:
transfer() {
wget -t 1 -qO - --method=PUT --body-file="$1" --header="Content-Type: $(file -b --mime-type "$1")" https://transfer.sh/$(basename "$1");
echo
}
alias transfer=transfer
配置完成后,只需简单命令即可上传文件:
$ transfer test.txt
Fish Shell用户配置
Fish Shell用户可以使用以下配置:
curl版本:
function transfer --description 'Upload a file to transfer.sh'
if [ $argv[1] ]
set -l tmpfile ( mktemp -t transferXXXXXX )
curl --progress-bar --upload-file "$argv[1]" https://transfer.sh/(basename $argv[1]) >> $tmpfile
cat $tmpfile
command rm -f $tmpfile
else
echo 'usage: transfer FILE_TO_TRANSFER'
end
end
funcsave transfer
Windows用户配置
Windows用户可以创建一个transfer.cmd
文件并放入PATH路径中:
@echo off
setlocal
set FN=%~nx1
set FULL=%1
powershell -noprofile -command "$(Invoke-Webrequest -Method put -Infile $Env:FULL https://transfer.sh/$Env:FN).Content"
文件上传与下载技巧
多种上传方式
- 使用wget上传:
wget --method PUT --body-file=/tmp/file.tar https://transfer.sh/file.tar -O - -nv
- 使用PowerShell上传:
Invoke-Webrequest -method put -infile .\file.txt https://transfer.sh/file.txt
- 上传过滤后的文本:
grep 'pound' /var/log/syslog | curl --upload-file - https://transfer.sh/pound.log
下载文件
- 使用curl下载:
curl https://transfer.sh/1lDau/test.txt -o test.txt
- 使用wget下载:
wget https://transfer.sh/1lDau/test.txt
高级应用场景
数据备份与归档
- 备份MySQL数据库并上传:
mysqldump --all-databases | gzip | gpg -ac -o- | curl -X PUT --upload-file "-" https://transfer.sh/test.txt
- 压缩并上传目录:
tar -czf - /var/log/journal | curl --upload-file - https://transfer.sh/journal.tar.gz
- 批量上传多个文件:
curl -i -F filedata=@/tmp/hello.txt -F filedata=@/tmp/hello2.txt https://transfer.sh/
文件加密与解密
- 加密上传文件:
gpg --armor --symmetric --output - /tmp/hello.txt | curl --upload-file - https://transfer.sh/test.txt
- 下载并解密文件:
curl https://transfer.sh/1lDau/test.txt | gpg --decrypt --output /tmp/hello.txt
病毒扫描功能
- 上传文件并扫描病毒:
curl -X PUT --upload-file ./eicar.com https://transfer.sh/eicar.com/scan
- 上传文件到VirusTotal:
curl -X PUT --upload-file nhgbhhj https://transfer.sh/test.txt/virustotal
实用增强功能
自动复制下载命令
配置以下函数可以自动复制下载命令到剪贴板:
transfer() {
curl --progress-bar --upload-file "$1" https://transfer.sh/$(basename "$1") | pbcopy;
echo "1) Download link:"
echo "$(pbpaste)"
echo "\n2) Linux/macOS下载命令:"
linux_macos_download_command="wget $(pbpaste)"
echo $linux_macos_download_command
echo "\n3) Windows下载命令:"
windows_download_command="Invoke-WebRequest -Uri "$(pbpaste)" -OutFile $(basename $1)"
echo $windows_download_command
case $2 in
l|m) echo $linux_macos_download_command | pbcopy;;
w) echo $windows_download_command | pbcopy;;
esac
}
使用示例:
$ transfer ~/temp/a.log w
设置下载限制和保存期限
以下脚本允许设置文件下载次数和保存天数:
URLFILE=$HOME/temp/transfersh.url
if [ -f $1 ]; then
read -p "Allowed number of downloads: " num_down
read -p "Number of days on server: " num_save
curl -sD - -H "Max-Downloads: $num_down" -H "Max-Days: $num_save" --progress-bar --upload-file $1 https://transfer.sh/$(basename $1) | grep -i -E 'transfer\.sh|x-url-delete' &> $URLFILE
if [ -f $URLFILE ]; then
URL=$(tail -n1 $URLFILE)
TOKEN=$(grep delete $URLFILE | awk -F "/" '{print $NF}')
echo "URL: $URL"
echo "删除令牌: $TOKEN"
fi
fi
通过掌握这些技巧,您可以更高效地使用transfer.sh进行文件传输和管理,满足各种工作场景的需求。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考