Day 5文件管理—三剑客的了解

文件的下载 wget curl

1.文件的上传 rz sz #不支持拷贝文件夹
 文件内容进行 排序 sort ,去重uniq, 统计 文件的截取 cut awk sed .... |  
3.文件管理之:联网下载文件(wget、curl)、文件上传与下载(rz、sz) ---wget、curl联网下载文件
1.CentOS7 系统最小化安装默认没有wget命令,需要进行安装
 [root@oldboyedu ~]# yum install wget -y
2.找到我们需要下载的资源 复制资源的链接地址
3.在linux上使用wget命令进行下载(默认下载到当前目录来)
[root@oldboyedu ~]# wget [http://fj.xuliangwei.com/public/weixin.py](http://fj.xuliangwei.com/public/weixin.py)  
4.由于我们下载的是文件,所有我们可以使用cat less more 查看该 文件
 [root@oldboyedu ~]# cat weixin.py  
5.使用wget下载资源时,指定保存的位置,并重新命名
[root@oldboyedu ~]# wget -O /opt/tt.png [http://fj.xuliangwei.com/public/ks.jpeg](http://fj.xuliangwei.com/public/ks.jpeg)  
6.下载资源时,如果不想重新命名只想修改保存的路径,请带上原有的名称
[root@oldboyedu ~]# wget -O /opt/ks.jpeg [http://fj.xuliangwei.com/public/ks.jpeg](http://fj.xuliangwei.com/public/ks.jpeg)  

url----------------> curl 浏览网络上的资源

1.在线浏览网站资源内容(源代码)
[root@oldboyedu ~]# curl [http://fj.xuliangwei.com/public/weixin.py](http://fj.xuliangwei.com/public/weixin.py) 
2.使用curl将内容保存至本地,并重命名(如果没有明确指定路径,则表示 当前目录)
[root@oldboyedu ~]# curl -o wei.txt [http://fj.xuliangwei.com/public/weixin.py](http://fj.xuliangwei.com/public/weixin.py)  #3.将资源保存至指定的路径 
[root@oldboyedu ~]# curl -o /opt/weixin.py [http://fj.xuliangwei.com/public/weixin.py](http://fj.xuliangwei.com/public/weixin.py) 
PS: 通常情况下我们推荐使用wget下载,但由于系统很多时候默认没有 按照wget 会偶尔使用一下curl

rzsz上传下载文件

练习: 使用两种方式下载如下的两个文件
1.wget保存至本地 /etc/yum.repos.d/CentOS-Base.repo
[http://mirrors.aliyun.com/repo/Centos-7.repo] 
(http://mirrors.aliyun.com/repo/Centos-7.repo)
 [root@oldboyedu ~]#  wget -O /etc/yum.repos.d/CentOSBase.repo     
 [http://mirrors.aliyun.com/repo/Centos-7.repo] 
 (http://mirrors.aliyun.com/repo/Centos-7.repo)  
2.curl保存至本地 /etc/yum.repos.d/epel.repo
[http://mirrors.aliyun.com/repo/epel-7.repo]     
(http://mirrors.aliyun.com/repo/epel-7.repo) 
[root@oldboyedu ~]# curl -o /etc/yum.repos.d/epel.repo         
[http://mirrors.aliyun.com/repo/epel-7.repo]        

(http://mirrors.aliyun.com/repo/epel-7.repo)

3.最后执行一条命令检查 yum makecache #练习: 将阿里云的仓库源 该为 清华大学的源 https://mirrors.tuna.tsinghua.edu.cn 搜狐 网易 ===
 4.文件管理之:文件或命令查找(which、whereis、find)  ----------------------------------------命令查找---------------------------------------  
5.文件管理之:文件内容处理命令(sort、uniq、cut、sed、awk、 wc、)  ----------------------------------------sort排序---------------------------------------  #如果无法将文件直接拖拽进Linux服务器 #1.要么没有安装lrzsz ,建议安装 yum install lrzsz -y 
2.你上传的是一个空文件
rz #只能上传文件,不支持上传文件夹,不支持大于4个G上传,也不 支持断点续传 # sz /path/file #只能下载文件 (任意单个文件),不支持下载文件 夹
1.查找一个命令的绝对路径 #当我们想执行一个命令的绝对路径时,先使用which command 查询绝 对路径 # which ls #查找ls命令的绝对路径
2.whereis也使用来查询命令的绝对路径 # whereis ls //查找命令的路径、帮助手册、等 # whereis -b ls //仅显示命令所在的路径
3.对于内核相关的一些命令,使用which whereis是无法查询到,需要使 用type命令查询 # type -a ls #查看命令的绝对路径(包括别名) #对于后面要使用一个命令的绝对时. 在有些情况下,需要对应一个无序的文本文件进行数据的排序,这时 就需要使用sort进行排序了。 sort [OPTION]... [FILE]... # -r:倒序 -n:按数字排序 -t:指定分隔符(默认空格) -k:指定第几 列, 指定几列几字符(指定1,1 3.1,3.3)
1.首先创建一个文件,写入一写无序的内容
 [root@xuliangwei ~]# cat >> file.txt <<EOF b:3 c:2 a:4 e:5 d:1 f:11 EOF  
2.使用sort下面对输出的内容进行排序
 [root@xuliangwei ~]# sort file.txt a:4 b:3 c:2 d:1 e:5 f:11  #结果并不是按照数字排序,而是按字母排序。 #可以使用-t指定分隔符, 使用-k指定需要排序的列。
 [root@xuliangwei ~]# sort -t ":" -k2 sort.txt d:1 f:11 #第二行为什么是11?不应该按照顺序排列? c:2 b:3 a:4 e:5  ----------------------------------------uniq去重---------------------------------------如果文件中有多行完全相同的内容,当前是希望能删除重复的行,同 时还可以统计出完全相同的行出现的总次数, 那么就可以使用uniq命 令解决这个问题(但是必须配合sort使用)。  #按照排序的方式, 只会看到第一个字符,11的第一个字符是1, 按照字符 来排序确实比2小。 #如果想要按照数字的方式进行排序, 需要使用 -n参数。 
 [root@xuliangwei ~]# sort -t ":" -n -k2 p.txt d:1 c:2 b:3 a:4 e:5 f:11  # 
测试案例,下载文件 [http://fj.xuliangwei.com/public/ip.txt] 
(http://fj.xuliangwei.com/public/ip.txt),对该文件进行排序 [ 
 root@xuliangwei ~]# sort -t. -k3.1,3.1nr -k4.1,4.3nr ip.txt  uniq 
[OPTION]... [INPUT [OUTPUT]] #选项:-c 计算重复的行  #1.创建一个file.txt文件: [root@xuliangwei ~]# cat >>file1.txt <<EOF abc 123 
abc 123 EOF #2.uniq需要和sort一起使用, 先使用sort排序, 让重复内 
容连续在一 起  ----------------------------------------cut截取字段(不太重 
 要)--------------------------------------  [root@xuliangwei ~]# sort file.txt 
123 123 abc abc #3.使用uniq去除相邻重复的行
[root@xuliangwei ~]# sort file.txt |uniq 123 abc 
4.-c参数能统计出文件中每行内容重复的次数
 [root@xuliangwei ~]# sort file.txt |uniq -c 2 123 2 abc  
面试题: 请统计分析如下日志,打印出访问最高前10的IP cut OPTION... [FILE]... #选项:-d 指定分隔符 -f 数字,取第几列 –f3,6三列和6列 -c 按字符 取(空格也算)
[root@oldboyedu ~]# cat >>file2.txt <<EOF Im xlw, is QQ 552408925 EOF  
实现: 筛选出文件里 xlw以及552408925
 [root@oldboyedu ~]# awk '{print $2,$5}' file2.txt | awk -F "," '{print $1,$2}' xlw 552408925  [root@oldboyedu ~]# cut -d " " -f 2,5 file2.txt | awk -F "," '{print $1,$2}' xlw 552408925  ----------------------------------------wc统计行号--------------------------------------  习题: 分析如下日志,统计每个域名被访问的次数。
 [root@oldboyedu ~]# cut -d " " -f 2,5 file2.txt | sed 's#,##g' xlw 552408925 
 [root@oldboyedu ~]# sed 's#,##g' file2.txt | awk '{print $2,$5}' xlw 552408925  #PS: 实际生产使用过程中,很少使用到cut,通常都是使用awk,因为awk 是取列专业户  wc [OPTION]... [FILE]... #选项:-l显示文件行数
wc -l /etc/fstab #统计/etc/fstab文件有多少行 # wc -l /etc/services #统计/etc/services 文件行号
练习题: 过滤出/etc/passwd以nologin结尾的内容,并统计有多少行 #1.先筛选出目标的行 #2.然后进行统计 [root@oldboyedu ~]# grep "nologin$" /etc/passwd | wc l 18

扩展统计文件行号的方法

 [root@oldboyedu ~]# cat -n /etc/services | tail -1 
 [root@oldboyedu ~]# grep -n ".*" /etc/services | tail -1
习题: 使用awk取出系统的IP地址,思路如下: 1.我要取的值在哪里 ifconfig ens32 2.如何缩小取值范围(行) 3.如何精确具体内容(列)
[root@student tmp]# cat >> web.log <<EOF     
[http://www.xuliangwei.com/index.html]    
(http://www.xuliangwei.com/index.html)  
[http://www.xuliangwei.com/1.html]      
(http://www.xuliangwei.com/1.html)  
[http://post.xuliangwei.com/index.html] 
(http://post.xuliangwei.com/index.html)  
[http://mp3.xuliangwei.com/index.html] 
(http://mp3.xuliangwei.com/index.html)  
[http://www.xuliangwei.com/3.html] 
(http://www.xuliangwei.com/3.html)  
[http://post.xuliangwei.com/2.html](http://post.xuliangwei.com/2.html) 
EOF  
[root@oldboyedu ~]# cut -d "/" -f 3 web.log | sort | uniq -c | sort -nr 
[root@oldboyedu ~]# awk -F "/" '{print $3}' web.log |sort |uniq -c |sort -nr  
.先拿到结果,然后提取有关键字那一行,最后使用awk取出那一列
[root@oldboyedu ~]# ifconfig ens32|grep "netmask" | awk '{print $2}' 
10.0.0.200  [root@oldboyedu ~]# ifconfig ens32 | awk '/netmask/' | 
 awk '{print $2}' 10.0.0.200  [root@oldboyedu ~]# ifconfig ens32 | 
 awk 
'/netmask/ {print $2}' 10.0.0.200  习题:分析如下日志,请提取出访问次 
 数 
É高的TOP10IP地址  习题: 将/etc/passwd文件中的第一行中的第一列 
和É后一列位置进行 交换。  习题: 将/etc/sysconfig/selinux 文件中的 
SELINUX=enforcing替换成 SELINUX=disabled

2 今日内容 文件相关 1.wget curl 下载互联网上的文件

2.rz sz 下载linux服务器上的文件,或上传文件至linux服务器
3.which whereis type 查找一个命令的绝对路径
4.cut awk 取列 grep 取行 wc 统计内容
cut+awk awk grep+awk grep+wc

原文地址:https://www.cnblogs.com/baozexu/p/11372795.html