背景:
wget https://XXXX.tar.gz
sh: wget: command not found
原因:
子账户没有安装wget命令
解决方法:
采用curl -o 等方法是下载包。
详解:
在没有系统 wget
的情况下,有多种替代方法可以实现文件下载功能。以下是一些实用的解决方案:
一、命令行工具替代方案
1. 使用 curl
(最常用)
curl
是几乎所有 Linux/macOS 系统预装的工具,功能与 wget
类似:
# 下载文件并指定保存名称
curl -o filename.zip https://example.com/file.zip
# 下载并使用远程文件名
curl -O https://example.com/file.zip
# 显示进度条
curl -O -# https://example.com/file.zip
# 断点续传(添加 -C - 参数)
curl -C - -O https://example.com/large_file.zip
2. 使用 aria2c
(多线程高速下载)
若系统已安装 aria2c
(需手动安装):
# 安装(Ubuntu/Debian)
sudo apt install aria2
# 下载文件(支持多线程加速)
aria2c -x 16 https://example.com/file.zip
3. 使用 python3
内置模块
直接通过 Python 脚本下载(无需额外安装):
# 方法一:使用 urllib.request
python3 -c "import urllib.request; urllib.request.urlretrieve('https://example.com/file.zip', 'file.zip')"
# 方法二:使用 requests 库(需提前安装:pip3 install requests)
python3 -c "import requests; r = requests.get('https://example.com/file.zip'); open('file.zip', 'wb').write(r.content)"
二、编程语言替代方案
1. Bash + /dev/tcp
(原始 TCP 连接)
适用于极简环境(需支持 /dev/tcp
设备):
#!/bin/bash
URL="https://example.com/file.txt"
HOST=$(echo "$URL" | cut -d/ -f3)
PATH=$(echo "$URL" | cut -d/ -f4-)
exec 3<>/dev/tcp/$HOST/443 # 建立 TCP 连接(HTTP 使用 80 端口)
echo -e "GET /$PATH HTTP/1.1\nHost: $HOST\nConnection: close\n\n" >&3
cat <&3 > file.txt
exec 3>&- # 关闭连接
2. Perl 脚本
大多数系统预装 Perl:
perl -e 'use LWP::Simple; getstore("https://example.com/file.zip", "file.zip");'
三、其他实用方法
1. 浏览器下载(图形界面)
若有桌面环境,直接通过 Firefox/Chrome 访问 URL 下载。
2. SSH/SCP 从远程服务器获取
从其他服务器复制文件:
scp user@remote:/path/to/file.zip .
3. 使用 ftp
命令(针对 FTP 协议)
ftp -o filename.txt ftp://example.com/path/to/file.txt
四、选择建议
场景 | 推荐工具/方法 | 备注 |
---|---|---|
日常下载 | curl -O |
系统默认预装 |
大文件多线程下载 | aria2c |
需手动安装,速度更快 |
脚本化下载 | python3 -c "..." |
无需额外依赖 |
极简环境(无任何工具) | /dev/tcp + Bash |
需了解 HTTP 协议格式 |
如果需要长期使用下载功能,建议通过包管理器安装 wget
或 curl
:
# Ubuntu/Debian
sudo apt install wget curl
# CentOS/RHEL
sudo yum install wget curl