文件管理之:联网下载文件(wget、crul)、文件上传于下载(rz、sz)
wget常用选项及说明
选项 | 说明 |
---|---|
-O | 下载文件到指定目录 |
用wget下载http://mirrors.aliyun.com/repo/Centos-7.repo到当前目录
[root@node2 ~]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
--2019-07-30 10:03:04-- http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 39.156.4.172, 39.156.4.174, 39.156.4.176, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|39.156.4.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘Centos-7.repo’
100%[=========================================>] 2,523 --.-K/s in 0s
2019-07-30 10:03:04 (217 MB/s) - ‘Centos-7.repo’ saved [2523/2523]
[root@node2 ~]# ll
total 8
-rw-------. 1 root root 1416 Jul 24 23:41 anaconda-ks.cfg
-rw-r--r--. 1 root root 2523 Jun 16 2018 Centos-7.repo
[root@node2 ~]#
将下载http://mirrors.aliyun.com/repo/Centos-7.repo到/mnt目录下,保持文件名称不变
[root@node2 ~]# wget -O /mnt/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 若想保持文件名称不变 ,在下载路径中要指定原名,否则不能下载成功
--2019-07-30 09:44:18-- http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.32.169.243, 111.32.169.245, 111.32.169.248, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.32.169.243|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/mnt/Centos-7.repo’
100%[=========================================>] 2,523 --.-K/s in 0s
2019-07-30 09:44:18 (250 MB/s) - ‘/mnt/Centos-7.repo’ saved [2523/2523]
[root@node2 ~]# ll /mnt/
total 4
-rw-r--r--. 1 root root 2523 Jun 16 2018 Centos-7.repo
将下载http://mirrors.aliyun.com/repo/Centos-7.repo到/mnt目录下,并重命名为test.txt
[root@node2 ~]# wget -O /mnt/test.txt http://mirrors.aliyun.com/repo/Centos-7.repo
--2019-07-30 09:47:44-- http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.32.169.245, 111.32.169.248, 111.32.169.249, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.32.169.245|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/mnt/test.txt’
100%[=========================================>] 2,523 --.-K/s in 0s
2019-07-30 09:47:44 (207 MB/s) - ‘/mnt/test.txt’ saved [2523/2523]
[root@node2 ~]# ll /mnt
total 4
-rw-r--r--. 1 root root 2523 Jun 16 2018 test.txt
curl常用选项及说明
选项 | 说明 |
---|---|
-o | 下载文件到指定目录 |
用curl查看http://mirrors.aliyun.com/repo/Centos-7.repo内容
[root@node2 ~]# curl http://mirrors.aliyun.com/repo/Centos-7.repo
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
用curl下载http://mirrors.aliyun.com/repo/Centos-7.repo并保存到/mnt下,并重命名为test.txt
[root@node2 ~]# curl -o /mnt/test.txt http://mirrors.aliyun.com/repo/Centos-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2523 100 2523 0 0 18920 0 --:--:-- --:--:-- --:--:-- 18969
[root@node2 ~]# ll /mnt/
total 4
-rw-r--r--. 1 root root 2523 Jul 30 09:58 test.txt
通常情况下,下载文件使用wget命令,但是最小化安装系统默认不安装wget,可以使用curl下载。
rz 和 sz
rz用于从Windows上传文件至Linux上,直接将文件拖拽到bash shell端口就行

sz用于从Linux服务器上下载文件至Windows上,其格式为sz + 文件名称

rz:只能上传文件,不支持上传文件夹,不支持大于4G上传,不支持断点续传。
sz:只能下载文件(任意单个文件),不支持下载文件夹。
文件管理之:文件或命令查找(which 、whereis)
which :可用于查看命令文件的绝对路径及别名
[root@node2 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
whereis :可用于查看命令的文件的绝对路径及帮助文档路径
[root@node2 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
type :可用于查看命令的类型、文件位置、别名等信息
[root@node2 ~]# type ls
ls is aliased to `ls --color=auto'
[root@node2 ~]# type for
for is a shell keyword
[root@node2 ~]# type cd
cd is a shell builtin
文件管理之:文件内容处理命令(sort、uniq、cut、sed、awk、wc)
sort常用选项及说明
选项 | 说明 |
---|---|
-n | 依照数值大小进行排序 |
-r | 以相反的顺序显示 |
-t | 指定分隔符 |
-k | 指定要取的字符 |
源文件
[root@node2 ~]# cat file.txt
b:3
c:2
a:4
e:5
d:1
f:11
将文件排序首列进行排序
[root@node2 ~]# sort file.txt
a:4
b:3
c:2
d:1
e:5
f:11
将文本第二列数字按照大小进行排序
[root@node2 ~]# sort -t ":" -n -k2 file.txt
# -t指定分隔符,-n以数字大小进行排序,-k指定要排序的列
d:1
c:2
b:3
a:4
e:5
f:11
源文件
[root@node2 ~]# cat 1.txt
10.0.3.1 00:0F:AF:81:19:1F
10.0.3.2 00:0F:AF:85:6C:25
10.0.3.3 00:0F:AF:85:70:42
10.0.2.20 00:0F:AF:85:55:DE
10.0.2.21 00:0F:AF:85:6C:09
10.0.2.22 00:0F:AF:85:5C:41
10.0.0.151 00:0F:AF:85:6C:F6
10.0.0.152 00:0F:AF:83:1F:65
10.0.0.153 00:0F:AF:85:70:03
10.0.1.10 00:30:15:A2:3B:B6
10.0.1.11 00:30:15:A3:23:B7
10.0.1.12 00:30:15:A2:3A:A1
10.0.1.1 00:0F:AF:81:19:1F
10.0.2.2 00:0F:AF:85:6C:25
10.0.3.3 00:0F:AF:85:70:42
10.0.2.20 00:0F:AF:85:55:DE
10.0.1.21 00:0F:AF:85:6C:09
10.0.2.22 00:0F:AF:85:5C:41
10.0.0.151 00:0F:AF:85:6C:F6
10.0.1.152 00:0F:AF:83:1F:65
10.0.0.153 00:0F:AF:85:70:03
10.0.3.10 00:30:15:A2:3B:B6
10.0.1.11 00:30:15:A3:23:B7
10.0.3.12 00:30:15:A2:3A:A1
对该文件输出内容进行排序(提示:通过第三列的第一个字符,以及第4列的所有字符进行排序)
[root@node2 ~]# sort -t "." -n -k3.1,3.1 -k4.1,4.3 1.txt
uniq常用选项及说明
选项 | 说明 |
---|---|
-c | 计算重复的行 |
源文件
[root@node2 ~]# cat file.txt
123
abc
123
abc
123
用uniq去重,并显示重复次数
[root@node2 ~]# sort file.txt | uniq -c
3 123
2 abc
要想用uniq去重,必须先用sort将重复的内容排列到一起。
cut常用选项及说明
选项 | 说明 |
---|---|
-d | 指定分隔符 |
-f | 取第几列 |
取用户名及QQ号
[root@node2 ~]# cat file.txt
I am Jack MY QQ is 793499077
[root@node2 ~]# cut -d " " -f 3,7 file.txt
Jack 793499077
cut几乎很少用到,通常都使用awk,因为awk功能更全。
wc常用选项及说明
选项 | 说明 |
---|---|
-l | 显示文件的行数 |
统计/etc/services文件的行数
[root@node2 ~]# wc -l /etc/services
11176 /etc/services
awk常用选项及说明
选项 | 说明 |
---|---|
-F | 指定分隔符 |
- 取出设备的ip地址
[root@node2 ~]# ifconfig | grep broadcast | awk '{print $2}'
10.0.0.201
- 取出访问日志服务器的top10的IP地址。
源文件部分内容如下:
[root@node2 ~]# tail -20 fj.xuliangwei.com.log
122.71.240.181 - - [30/Jul/2019:11:21:12 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"14"
122.71.240.181 - - [30/Jul/2019:11:21:45 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"3"
122.71.240.181 - - [30/Jul/2019:11:22:10 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"2"
122.71.240.181 - - [30/Jul/2019:11:22:32 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"2"
122.71.240.181 - - [30/Jul/2019:11:22:39 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"2"
122.71.240.181 - - [30/Jul/2019:11:23:18 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "curl/7.29.0" "-" -"4"
122.71.240.181 - - [30/Jul/2019:11:23:20 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"3"
122.71.240.181 - - [30/Jul/2019:11:23:57 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"4"
122.71.240.181 - - [30/Jul/2019:11:23:59 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"4"
122.71.240.181 - - [30/Jul/2019:11:24:09 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"3"
122.71.240.181 - - [30/Jul/2019:11:24:16 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"4"
61.129.6.227 - - [30/Jul/2019:11:25:17 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; rv:11.0) like Gecko" "-" -"9"
101.89.239.232 - - [30/Jul/2019:11:25:43 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36" "-" -"8"
58.247.204.63 - - [30/Jul/2019:11:25:43 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2141.400 QQBrowser/9.5.10219.400" "-" -"9"
101.91.60.108 - - [30/Jul/2019:11:25:44 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-" -"10"
122.71.240.181 - - [30/Jul/2019:11:25:47 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"11"
122.71.240.181 - - [30/Jul/2019:11:26:21 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "curl/7.29.0" "-" -"4"
122.71.240.181 - - [30/Jul/2019:11:28:37 +0800] "GET /public/ip.txt HTTP/1.1" 0.000 200 744 "-" "Wget/1.14 (linux-gnu)" "-" -"6"
42.236.10.117 - - [30/Jul/2019:12:03:05 +0800] "GET /public/weixin.py HTTP/1.1" 0.000 200 1350 "-" "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-CN; EML-AL00 Build/HUAWEIEML-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 baidu.sogo.uc.UCBrowser/11.9.4.974 UWS/2.13.1.48 Mobile Safari/537.36 AliApp(DingTalk/4.5.11) com.alibaba.android.rimet/10487439 Channel/227200 language/zh-CN" "-" -"2"
42.236.10.106 - - [30/Jul/2019:12:03:11 +0800] "GET /public/weixin.py HTTP/1.1" 0.000 200 1350 "http://www.baidu.com/" "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-CN; EML-AL00 Build/HUAWEIEML-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 baidu.sogo.uc.UCBrowser/11.9.4.974 UWS/2.13.1.48 Mobile Safari/537.36 AliApp(DingTalk/4.5.11) com.alibaba.android.rimet/10487439 Channel/227200 language/zh-CN" "-" -"3"
取出访问日志服务器的top10的IP地址。
[root@node2 ~]# awk '{print $1}' fj.xuliangwei.com.log | sort | uniq -c | sort -nr | head -10
# 通过awk取出带有IP地址的列,排序后统计,将统计的结果以倒叙的方式显示,用head控制,显示前10行
4432 139.226.173.42
483 139.226.172.150
381 139.226.172.192
193 122.71.240.181
102 223.167.152.124
100 61.153.150.107
95 47.97.98.158
83 45.195.91.193
83 125.71.215.239
79 114.87.94.145
网友评论