Linux文件传输与操作命令详解

一、跨平台文件传输方案

1. Windows ⇄ Linux

  • 推荐工具: MobaXterm工具上传和下载
  • 使用场景: 图形化界面操作,简单便捷

2. Linux ⇄ Linux

  • 推荐命令: scp, rsync, curl, wget等
  • 使用场景: 命令行操作,适合自动化和脚本

二、SCP命令(安全复制)

基本语法

1
scp [选项] 源文件 目标地址

常用选项

  • -r: 递归复制整个目录
  • -P port: 指定端口号(默认22)
  • -p: 保留原文件的修改时间、访问时间和权限

使用示例

1. 复制文件到远程Linux主机

1
2
3
4
# 复制文件到远程主机的家目录
[root@myserver ~]# scp File.cfg root@11.0.1.136:~/
root@11.0.1.136's password:
File.cfg 100% 1673 2.7MB/s 00:00

2. 复制目录到远程Linux主机

1
2
3
4
5
6
7
8
9
10
# 复制整个目录到远程主机
[root@myserver ~]# scp -r dir root@11.0.1.135:~/
The authenticity of host '11.0.1.135 (11.0.1.135)' can't be established.
ECDSA key fingerprint is SHA256:lahzXojSmiJvX2y1cHb4/ERNZzETnFedjbVdrdE0P6s.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '11.0.1.135' (ECDSA) to the list of known hosts.
root@11.0.1.135's password:
file1 100% 0 0.0KB/s 00:00
file2 100% 0 0.0KB/s 00:00
...

注意事项

  • 首次连接需要确认主机密钥
  • 需要输入远程主机的密码
  • 路径可以是绝对路径或相对路径

三、CURL命令(网络访问)

基本语法

1
curl [选项] [URL]

常用选项

  • -o 文件名: 将输出写入到指定文件
  • -O: 使用URL中的文件名保存到本地
  • -s: 静默模式,不显示进度信息
  • -I: 只显示HTTP头信息

使用示例

1. 获取网页源码

1
2
3
[root@myserver ~]# curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK-->...完整HTML代码...

2. 保存网页到文件

1
2
3
4
[root@myserver ~]# curl -o baidu.html www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 25299 0 --:--:-- --:--:-- --:--:-- 25602

3. 下载网络资源

1
2
3
4
[root@myserver ~]# curl -O https://www.linuxprobe.com/docs/LinuxProbe.pdf
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 22.2M 100 22.2M 0 0 779k 0 0:00:29 0:00:29 --:--:-- 1450k

四、RSYNC命令(文件同步)

基本语法

1
rsync [选项] 源文件 目标文件

常用选项

  • -a: 归档模式,保持所有文件属性
  • -v: 显示详细信息
  • -z: 传输时进行压缩
  • -P: 显示进度和保持部分传输的文件
  • -r: 递归复制目录

使用示例

1. 同步单个文件到远程主机

1
2
3
4
5
6
7
8
[root@myserver ~]# rsync -avzP File.cfge root@11.0.1.136:~/
root@11.0.1.136's password:
sending incremental file list
File.cfge
1,660 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1)

sent 1,019 bytes received 35 bytes 110.95 bytes/sec
total size is 1,660 speedup is 1.57

2. 同步整个目录到远程主机

1
2
3
4
5
6
7
8
9
10
11
[root@myserver ~]# rsync -avzP dir root@11.0.1.136:~/
root@11.0.1.136's password:
sending incremental file list
dir/
dir/File.cfg
1,673 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/8)
dir/File.cfge
1,660 100% 1.58MB/s 0:00:00 (xfr#2, to-chk=5/8)
...
sent 2,300 bytes received 142 bytes 287.29 bytes/sec
total size is 3,333 speedup is 1.36

3. 同步本地文件

1
rsync -avSH /var/log ./log

思考题解答

1
2
# 将远程文件同步到本地
rsync -avzP user@remote:/path/to/file ./local/

五、WGET命令(网络下载)

基本语法

1
wget [选项] [URL]

常用选项

  • -c: 断点续传
  • -r: 递归下载
  • -np: 不追踪父级目录
  • -nH: 不创建主机目录
  • -O 文件名: 指定输出文件名

使用示例

1. 下载单个文件

1
# wget https://repo.mysql.com//mysql80-community-release-el7-11.noarch.rpm -O ./mysql.rpm

2. 断点续传下载

1
2
3
4
5
6
7
[root@myserver ~]# wget -c https://repo.mysql.com//mysql80-community-release-el7-11.noarch.rpm
--2023-11-28 15:10:17-- https://repo.mysql.com//mysql80-community-release-el7-11.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 23.210.109.97
正在连接 repo.mysql.com (repo.mysql.com)|23.210.109.97|:443... 已连接。
...
100%[=====================================================================================>] 14,064 --.-K/s 用时 0s
2023-11-28 15:10:18 (184 MB/s) - 已保存 "mysql80-community-release-el7-11.noarch.rpm" [14064/14064]

3. 下载整个网站

1
wget -r -np -nH --cut-dirs=1 -R index.html news.baidu.com

参数解释:

  • -r: 递归下载
  • -np: 不追踪父级目录
  • -nH: 不创建主机目录
  • –cut-dirs=1: 从URL中删除前1个目录
  • -R index.html: 排除index.html文件

六、LN命令(链接创建)

硬链接 vs 软链接

硬链接:

  • 与原文件共享相同的inode节点
  • 删除原文件不影响硬链接
  • 不能跨文件系统创建
  • 不能链接目录

软链接:

  • 创建新的inode节点
  • 存储指向目标文件的路径
  • 原文件删除后链接失效
  • 可以跨文件系统
  • 可以链接目录

创建示例

1. 创建硬链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 创建目录
[root@myserver ~]# mkdir hardlink

# 复制文件
[root@myserver ~]# cp anaconda-ks.cfg File.cfg

# 创建硬链接
[root@myserver ~]# ln File.cfg hardlink/hardlink.cfg

# 查看inode号(相同)
[root@myserver hardlink]# stat hardlink.cfg
文件:"hardlink.cfg"
大小:1660 块:8 IO 块:4096 普通文件
设备:fd00h/64768d Inode:2383252 硬链接:2
...
[root@myserver hardlink]# stat ../File.cfg
文件:"../File.cfg"
大小:1660 块:8 IO 块:4096 普通文件
设备:fd00h/64768d Inode:2383252 硬链接:2
...

# 删除原文件后硬链接仍可用
[root@myserver hardlink]# rm ../File.cfg
rm:是否删除普通文件 "../File.cfg"?y
[root@myserver hardlink]# ll
总用量 4
-rw-------. 1 root root 1660 11月 28 11:54 hardlink.cfg
[root@myserver hardlink]# head hardlink.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
...

2. 创建软链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 创建软链接
[root@myserver ~]# ln -s File.cfg softlink/softlink.cfg

# 查看inode号(不同)
[root@myserver ~]# stat File.cfg
文件:"File.cfg"
大小:1660 块:8 IO 块:4096 普通文件
设备:fd00h/64768d Inode:2485910 硬链接:1
...
[root@myserver ~]# stat softlink/softlink.cfg
文件:"softlink/softlink.cfg" -> "File.cfg"
大小:8 块:0 IO 块:4096 符号链接
设备:fd00h/64768d Inode:9119593 硬链接:1
...

# 删除原文件后软链接失效
[root@myserver ~]# rm File.cfg
rm:是否删除普通文件 "File.cfg"?y
[root@myserver ~]# cat softlink/softlink.cfg
cat: softlink/softlink.cfg: 没有那个文件或目录

关键区别对比

特性 硬链接(Hard Link) 软链接(Symbolic Link)
跨分区 ❌ 不支持 ✅ 支持
依赖 inode ✅ 是 ❌ 否(依赖路径)
源文件删除 仍可访问(数据未删除) ❌ 失效(悬空链接)
链接目录 ❌ 不支持 ✅ 支持

七、TAR命令(归档压缩)

基本语法

1
tar [选项] 文件名 [文件或目录]

常用选项

  • -c: 创建新的归档文件
  • -x: 从归档文件中提取文件
  • -v: 显示详细信息
  • -f: 指定归档文件名
  • -z: 通过gzip压缩或解压缩
  • -j: 通过bzip2压缩或解压缩
  • -Z: 通过compress压缩或解压缩
  • -C: 指定解压目录
  • -t: 列出归档文件中的内容

使用示例

1. 创建压缩包

1
2
3
4
5
6
7
8
# 使用gzip压缩
[root@myserver ~]# tar -cvzf anaconda-ks.tar.gz anaconda-ks.cfg
anaconda-ks.cfg
[root@myserver ~]# ll
总用量 12
-rw-r--rw-. 1 centos centos 1660 11月 22 17:35 anaconda-ks.cfg
-rw-r--r--. 1 root root 1067 11月 29 10:21 anaconda-ks.tar.gz
-rw-r--r--. 1 root root 1708 11月 22 17:39 initial-setup-ks.cfg

2. 解压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
# 删除原文件
[root@myserver ~]# rm anaconda-ks.cfg
rm:是否删除普通文件 "anaconda-ks.cfg"?y

# 解压文件
[root@myserver ~]# tar -xvzf anaconda-ks.tar.gz
anaconda-ks.cfg

[root@myserver ~]# ll
总用量 12
-rw-r--rw-. 1 centos centos 1660 11月 22 17:35 anaconda-ks.cfg
-rw-r--r--. 1 root root 1067 11月 29 10:21 anaconda-ks.tar.gz
-rw-r--r--. 1 root root 1708 11月 22 17:39 initial-setup-ks.cfg

注意事项

  • 解压时需要与压缩时使用相同的算法
  • 可以使用不同选项组合实现不同功能

八、命令对比总结

命令 主要用途 跨平台 特点
scp Linux间文件复制 简单易用,需要密码或密钥
rsync 文件同步 增量同步,适合备份
curl 网络访问 功能强大,支持多种协议
wget 网络下载 简单直接,适合下载文件
ln 创建链接 创建硬链接或软链接
tar 归档压缩 打包和压缩文件