Day 03 Linux文件管理高级
一、文本处理三剑客命令
三剑客命令将在shell编程里深入讲解,此处先学会最基本的使用
1.sed
流式编辑器,主要擅长对文件的编辑操作,我们可以事先定制好编辑文件的指令,然后让sed自动完成对文件的整体编辑
1.1 用法
sed 选项 '定位+命令' 文件路径
1.2 选项
-
-n 取消默认输出
sed -n '1p;2p;4p' 1.txt # 带 n 只打印需要的部分 --------------------------------- user12312 23123user 111asda2323 sed '1p;2p;4p' 1.txt # 在默认输出基础上附加输出 --------------------------------- user12312 user12312 23123user 23123user 1123user12323 111asda2323 111asda2323 2131asdasd23423asdas35454dxcz 00000user9999user8888user
-
-r 支持扩展正则元字符(由于尚未学习正则,所以此处暂作了解)
-
-i 立即编辑文件
1.3 定位
行定位:
- 定位到第一行
- 1,3代表从第一行到第三行
- 不写定位代表定位所有的行
正则表达式定位:
/user/
包含user
的行/^user/
以user
开头的行/user$/
以user
结尾的行
数字+正则表达式定位:
-
1, 8p
代表打印1到8行sed -n '1,7p' 1.txt # 文本就6行 ---------------------------------- user12312 23123user 1123user12323 111asda2323 2131asdasd23423asdas35454dxcz 00000user9999user8888user
-
1, /user/p
则代表取从第一行到首次匹配到/user/
的行sed -n '1, /231/p' 1.txt # 打印1到以231 开头的行 ---------------------------------- user12312 23123user
1.4 命令
-
d 删除
sed '1d;2d;5d' 1.txt ----------------------------------- 1123user12323 111asda2323 00000user9999user8888user sed '1,4d' 1.txt # 删除1-4 所以只剩5 6了 ----------------------------------- 2131asdasd23423asdas35454dxcz 00000user9999user8888user
-
p 行数
sed -n '1p;2p;4p' 1.txt ------------------------------------ user12312 23123user 111asda2323 sed -n '1, /8user$/p' 1.txt # 输出第一行到 以8user结尾的内容 ------------------------------------ user12312 23123user 1123user12323 111asda2323 2131asdasd23423asdas35454dxcz 00000user9999user8888user
-
s///g
sed 's/user/SuperUser/g' 1.txt # 把所有行的user都替换为SuperUser g是一个声明相当于 global 把行所有的修改,不加只替换没行中的一个 ---------------------------------- SuperUser12312 23123SuperUser 1123SuperUser12323 111asda2323 2131asdasd23423asdas35454dxcz 00000SuperUser9999SuperUser8888SuperUser sed 's/user/SuperUser/' 1.txt # 没有g -------------------------------- SuperUser12312 23123SuperUser 1123SuperUser12323 111asda2323 2131asdasd23423asdas35454dxcz 00000SuperUser9999user8888user sed 's/^user/SuperHero/g' 1.txt # 以user开头的行中的user换成SuperHero ------------------------------- SuperHero12312 23123user 1123user12323 111asda2323 2131asdasd23423asdas35454dxcz 00000user9999user8888user sed '1, 4s/user/Waibi/g' 1.txt # 替换1-4行user为waibi ------------------------------ Waibi12312 23123Waibi 1123Waibi12323 111asda2323 2131asdasd23423asdas35454dxcz 00000user9999user8888user # 加上-i选项,直接修改文件,通常会在调试完毕确保没有问题后再加-i选项 sed '1, 4s/user/Waibi/g' 1.txt -i cat 1.txt ------------------------------ Waibi12312 23123Waibi 1123Waibi12323 111asda2323 2131asdasd23423asdas35454dxcz 00000user9999user8888user
命令可以用;号连接多多条,如1d;3d;5d代表删除1,3,5行
2.awk
awk主要用于处理有格式的文本,例如
/etc/passwd
这种,事实上awk是一门编程语言,可以独立完成很强大的操作,我们将在shell编程中详细介绍
1.1 用法
awk 选项 'pattern{action}' 文件路径
1.2 选项
-F 指定行分隔符
1.3 工作流程
awk -F: '{print $1,$3}' /etc/passwd
- awk会读取文件的一行内容然后赋值给$0
- 然后awk会以-F指定的分隔符将该行切分成n段,最多可以达到100段,第一段给$1,第二段给$2,依次次 类推
- print输出该行的第一段和第三段,逗号代表输出分隔符,默认与-F保持一致
- 重复步骤1,2,3直到文件内容读完
1.4 内置变量
$0
一整行内容
NR
记录号,等同于行号
NF
以-F分隔符分隔的段数
1.5pattern可以是
-
/正则/
/正则/ # 该行内容匹配成功正则
$1 ~ /正则/ # 第一段内容匹配成功正则
$1 !~ /正则/ # 第一段内容没有匹配成功正则
- 比较运算:
NR >= 3 && NR <=5 # 3到5行
$1 == "root" # 第一段内容等于root
1.6action可以是
print $1,$3
1.7 使用
cat 2.txt # 已编辑好的文本
---------------------------------
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
3.grep
1.1 用法
grep 选项 '正则' 文件路径
1.2 选项
- -n, --line-number 在过滤出的每一行前面加上它在文件中的相对行号
- -i, --ignore-case 忽略大小写
- –color 颜色
- -l, --files-with-matches 如果匹配成功,则只将文件名打印出来,失败则不打印 通常-rl一起用,grep -rl ‘root’ /etc
- -R, -r, --recursive 递归
1.3 示例
grep '^root' /etc/passwd # 过滤在/etc/passwd 文件下以root开头的文件
-------------------------------
root:x:0:0:root:/root:/bin/bash
grep -n '^root' /etc/passwd # 过滤在/etc/passwd 文件下以root开头的文件
-------------------------------
1:root:x:0:0:root:/root:/bin/bash # 行数
grep -n 'bash$' /etc/passwd # 过滤在/etc/passwd 文件下以bashjiewei的文件
-------------------------------
1:root:x:0:0:root:/root:/bin/bash
grep -rl '^root' /etc # 匹配 etc文件夹下 以root开头的文件 匹配成功打印出来 不成功不打印
# grep 也支持管道,我们可以发现三剑客命令都支持管道
[root@localhost ~]# ps aux |grep ssh
root 968 0.0 0.2 112908 4312 ? Ss 14:05 0:00 /usr/sbin/sshd
-D
root 1305 0.0 0.3 163604 6096 ? Ss 14:05 0:00 sshd:
root@pts/0
root 1406 0.0 0.3 163600 6240 ? Ss 14:05 0:00 sshd:
root@pts/1
root 2308 0.0 0.0 112724 984 pts/1 R+ 15:30 0:00 grep --
color=auto ssh
[root@localhost ~]# ps aux |grep [s]sh
root 968 0.0 0.2 112908 4312 ? Ss 14:05 0:00 /usr/sbin/sshd
-D
root 1305 0.0 0.3 163604 6096 ? Ss 14:05 0:00 sshd:
root@pts/0
root 1406 0.0 0.3 163600 6240 ? Ss 14:05 0:00 sshd:
root@pts/1
二、文件管理 -文件查找
1.查看命令所属文件
[root@hecs-x-medium-2-linux-20201118090009 home]# which ip
/usr/sbin/ip
[root@hecs-x-medium-2-linux-20201118090009 home]# whereis ip
ip: /usr/sbin/ip /usr/share/man/man8/ip.8.gz
2.查找文件
find [options] [path...] [expression]
按文件名
[root@localhost ~]# find /etc -name "ifcfg-eth0"
[root@localhost ~]# find /etc -iname "ifcfg-eth0" # -i忽略大小写
[root@localhost ~]# find /etc -iname "ifcfg-eth*"
按文件大小
[root@localhost ~]# find /etc -size +3M # 大于3M
[root@localhost ~]# find /etc -size 3M
[root@localhost ~]# find /etc -size -3M
[root@localhost ~]# find /etc -size +3M -ls # -ls找到的处理动作
指定查找的目录深度:
-maxdepth levels
[root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg-eth0" # -a并且,-o或者,
不加-a,默认就是-a
按时间查找(atime,mtime,ctime):
这个基本上看不出来啥差距,因为我们没有修改
[root@localhost ~]# find /etc -mtime +3 # 修改时间超过3天
[root@localhost ~]# find /etc -mtime 3 # 修改时间等于3天
[root@localhost ~]# find /etc -mtime -3 # 修改时间3天以内
按文件属主、属组找:
[root@localhost ~]# find /home -user egon # 属主是egon的文件
[root@localhost ~]# find /home -group it # 属组是it组的文件
[root@localhost ~]# find /home -user egon -group it
[root@localhost ~]# find /home -user egon -a -group it # 同上意思一样
[root@localhost ~]# find /home -user egon -o -group it
[root@localhost ~]# find /home -nouser # 用户还存在,在/etc/passwd中删除了记录
[root@localhost ~]# find /home -nogroup # 用户还存在,在/etc/group中删除了记录
[root@localhost ~]# find /home -nouser -o -nogroup
按文件类型:
[root@localhost ~]# find /dev -type f # f普通
[root@localhost ~]# find /dev -type d # d目录
[root@localhost ~]# find /dev -type l # l链接
[root@localhost ~]# find /dev -type b # b块设备
[root@localhost ~]# find /dev -type c # c字符设备
[root@localhost ~]# find /dev -type s # s套接字
[root@localhost ~]# find /dev -type p # p管道文件
根据inode号查找:-inum n
[root@localhost ~]# find / -inum 1811
按文件权限
[root@localhost ~]# find . -perm 644 -ls
[root@localhost ~]# find . -perm -644 -ls
[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find /sbin -perm -4000 -ls # 包含set uid
[root@localhost ~]# find /sbin -perm -2000 -ls # 包含set gid
[root@localhost ~]# find /sbin -perm -1000 -ls # 包含sticky
找到后处理的动作:
-print
-ls
-delete
-exec
-ok
[root@localhost ~]# find /etc -name "ifcfg*" -print # 必须加引号
[root@localhost ~]# find /etc -name "ifcfg*" -ls
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; # 非交互
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; # 交互 每个都要yes或者y
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
[root@localhost ~]# find /etc -name "ifcfg*" -delete # 同上
三、文件上传和下载
1.下载
wget命令
wget -O 本地路径 远程包链接地址 # 将远程包下载到本地,-O指定下载到哪里,可以生路-O 本地路径
# ps:如果wget下载提示无法建立SSL连接,则加上选项--no-check-certificate
wget --no-check-certificate -O 本地路径 远程包链接地址
curl命令
#curl命令是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。作为一款强力工具,curl支持包括HTTP、HTTPS、[ftp]等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。做网页处理流程和数据检索自动化,curl可以祝一臂之力。
[root@localhost ~]# curl -o 123.png https://www.xxx.com/img/hello.png
# ps: 如果遇到下载提示无法简历SSL链接,使用-k选项或者--insecure
curl -k -o name.jpg https://www.bing.com/th?id=OHR.BernCH_ZH-CN0890742909_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=HpEdgeAn
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dEeFs3BR-1606126022318)(https://i.loli.net/2020/11/23/9qFoasYSEmQjcLi.png)]
sz 命令 这个命令需要安装 lrzsz
系统默认没有该命令,需要下载:yum install lrzsz -y # 将服务器上选定的文件下载/发送到本机
2 上传
rz 命令 这个默认也是没有的 需要安装
# 系统默认没有该命令,需要下载:yum install lrzsz -y
# 运行该命令会弹出一个文件选择窗口,从本地选择文件上传到服务器。
[root@localhost opt]# rz # 如果文件已经存,则上传失败,可以用-E选项解决
[root@localhost opt]# rz -E # -E如果目标文件名已经存在,则重命名传入文件。新文件名将添加一
个点和一个数字(0..999)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SjBUIXCp-1606126022322)(https://i.loli.net/2020/11/23/V4LzBTuXAECFIHc.png)]
四 文件管理之:输入与重定向
五 文件管理之:字符处理命令
1.sort命令
用于将文件内容加以排序
- -n # 依照数值的大小排序
- -r # 以相反的顺序来排序
- -k # 以某列进行排序
- -t # 指定分割符,默认是以空格为分隔符
准备文件,写入一段无需内容
cat >> test.txt <<GG # 打开test.txt文件 追加写入(>>)内容 出现 GG时 结束写入
> b:3
> c:2
> a:4
> e:5
> d:1
> f:11
> GG
基本排序
sort test.txt # 默认是以第一个排序的 不区分大小写
---------------------------
a:4
b:3
c:2
d:1
e:5
f:11
Z:77
sort test.txt -r # 倒序 == sort -r test.txt
Z:77
f:11
e:5
d:1
c:2
b:3
a:4
选择排序内容
sort -t ':' -n -k2 test.txt # 以':'作为分隔(-t) 用分隔后的第二个(k2)数字(-n)元素 正向排序
-------------------------------------
d:1
c:2
b:3
a:4
e:5
f:11
Z:77
# 逆序同理
2.uniq命令 唯一
用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用
- -c # 在每列旁边显示该行重复出现的次数。
- -d # 仅显示重复出现的行列。
- -u # 仅显示出一次的行列。
准备文件,写入一段无序的内容
cat >> 2.txt <<GG
> hello
> 123
> hello
> 123
> func
> gg
> GG
基本使用
sort 2.txt
-----------------------------
123
123
func
gg
hello
hello
sort 2.txt |uniq # 不重复排序
----------------------------
123
func
gg
hello
-c 的使用 count 数量
sort 2.txt | uniq -c # 不重复排序 并显示出现次数
---------------------------
2 123
1 func
1 gg
2 hello
-d 的使用 多次出现? duo?
sort 2.txt | uniq -d
--------------------------
123
hello
-u 的使用 unique出现一次
sort 2.txt | uniq -u
-------------------------
func
gg
3.cut命令
cut命令用来显示行中的指定部分,删除文件中指定字段
- -d # 指定字段的分隔符,默认的字段分隔符为"TAB";
- -f # 显示指定字段的内容;
head -1 /etc/passwd
------------------------------
1 2 3 4 5 6 7
root:x:0:0:root:/root:/bin/bash
head -1 /etc/passwd | cut -d ':' -f1,2,4,6,7 # 上条显示的内容 以':'分隔(-d) 显示(-f)1,2,4,6,7这几个内容
------------------------------
root:x:0:/root:/bin/bash
5.4 tr命令
替换或删除命令
- -d # 删除字符
[root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT"
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@localhost ~]#
[root@localhost ~]# head -1 /etc/passwd |tr -d "root" # 删除 root字符
:x:0:0::/:/bin/bash
[root@localhost ~]# echo "hello egon qq:378533872" > a.txt
[root@localhost ~]# tr "egon" "EGON" < a.txt
hEllO EGON qq:378533872 # 吧egon替换为大写
5.5 wc命令
统计,计算数字
- -c # 统计文件的Bytes数;
- -l # 统计文件的行数;
- -w # 统计文件中单词的个数,默认以空白字符做为分隔符
[root@localhost ~]# ll file.txt
-rw-r--r--. 1 root root 25 8月 12 20:09 file.txt
[root@localhost ~]# wc -c file.txt
25 file.txt
[root@localhost ~]# cat file.txt
hello
123
hello
123
func
[root@localhost ~]# wc -l file.txt
5 file.txt
[root@localhost ~]# grep "hello" file.txt |wc -l
2
[root@localhost ~]# cat file.txt
hello
123
hello
123
func
[root@localhost ~]# wc -w file.txt
5 file.txt