第一章.管道
1.1 管道的概念
简单来说管道就是将前面命令处理的结果传递给后面的命令
1.2 管道与|xargs(管道xargs)的区别
find |xargs ls -ld##把前一个命令的结果,通过管道传递给后面的命令(ls -ld),传递的是文件名
find | 命令 ##把前一个命令的结果,通过管道传递给后面的命令,传递的是普通的文本,文字,字符串
第二章.find命令
1.1查找文件
涉及命令 find
创建模拟环境
touch /root/oldboy.txt /root/alex.txt /root/lidao.txt
1.2 在/root目录查找文件oldboy.txt
[root@oldboyedu-50 ~]# #find 在哪里找 -在哪找 f 找什么名字的 "oldboy.txt" [root@oldboyedu-50 ~]# find /root/ -type f -name "oldboy.txt"/root/oldboy.txt[root@oldboyedu-50 ~]# -type 找什么类型的 f file (文件) d directory(目录)
1.3 在/root目录查找以 .txt 结尾的文件
* 所有字符(文字) 任何文字
[root@oldboyedu-50 ~]# find /root/ -type f -name "*.txt"/root/alex.txt/root/lidao.txt/root/oldboy.txt[root@oldboyedu-50 ~]# ### *所有字符 任意字符[root@oldboyedu-50 ~]#
1.4 实现查找并删除
1.4.1 方法一
为防止误删除 不直接用rm删除 先用 ls -l查看一下内容 确认一下 确认后再删除
[root@oldboyedu-50 ~]# find /root/ -type f -name "*.txt"|xargs ls -l 查找并查看-rw-r--r--. 1 root root 0 Jul 10 19:42 /root/alex.txt-rw-r--r--. 1 root root 0 Jul 10 19:42 /root/lidao.txt-rw-r--r--. 1 root root 0 Jul 10 19:46 /root/oldboy.txt[root@oldboyedu-50 ~]# find /root/ -type f -name "*.txt"|xargs rm 查找并删除[root@oldboyedu-50 ~]# ls -l /root/ 检查是否删除total 40-rw-------. 1 root root 1161 Jul 10 18:26 anaconda-ks.cfg-rw-r--r--. 1 root root 21736 Jul 10 18:26 install.log-rw-r--r--. 1 root root 5890 Jul 10 18:25 install.log.syslog
1.4.2 方法二
因上面已删除 重新创建环境
touch /root/oldboy.txt /root/alex.txt /root/lidao.txt
首先为防止未删除 先查找并查看*.txt的文件
$
[root@oldboyedu-50 ~]# ls -l $(find /root/ -type f -name "*.txt")-rw-r--r--. 1 root root 0 Jul 10 18:47 /root/alex.txt-rw-r--r--. 1 root root 0 Jul 10 18:47 /root/lidao.txt-rw-r--r--. 1 root root 0 Jul 10 18:47 /root/oldboy.txt
确认无误 rm -f 删除
[root@oldboyedu-50 ~]# rm -f $(find /root/ -type f -name "*.txt") rm -f 强制删除不提示[root@oldboyedu-50 ~]# ls /root/ 检查是否删除anaconda-ks.cfg install.log install.log.syslog[root@oldboyedu-50 ~]#
1.4.3 方法三
同上先模拟创建环境
先查看搜索出的内容 然后再删除
[root@oldboyedu-50 ~]# find /root/ -type f -name "*.txt" -exec ls -l {} \; 查看搜索的文件-rw-r--r--. 1 root root 0 Jul 10 18:51 /root/alex.txt-rw-r--r--. 1 root root 0 Jul 10 18:51 /root/lidao.txt-rw-r--r--. 1 root root 0 Jul 10 18:51 /root/oldboy.txt[root@oldboyedu-50 ~]# find /root/ -type f -name "*.txt" -exec rm {} \; 删除搜索的文件[root@oldboyedu-50 ~]# ls /root/ 检查anaconda-ks.cfg install.log install.log.syslog[root@oldboyedu-50 ~]#
第三章 过滤信息
问题信息
已知文件 test.txt 内容为:
test
liyao
oldboy oldboy
请给出输出test.txt 文件内容时,不包含 oldboy 字符串的命令
首先模拟环境
[root@oldboyedu-50 ~]# mkdir -p /data[root@oldboyedu-50 ~]# cat > /data/test.txt <
3.1 方法一 grep
grep -v 清除搜索的内容 显示其他的
[root@oldboyedu-50 ~]# grep "oldboy" /data/test.txt oldboy[root@oldboyedu-50 ~]# grep -v "oldboy" /data/test.txt testliyao
3.2 方法二 head tail
head
head 默认显示文件的前几行内容 默认显示前十行
[root@oldboyedu-50 ~]# head -2 /data/test.txt -2 == -n2 在此处表示的参数是一样的testliyao[root@oldboyedu-50 ~]# head -n2 /data/test.txt testliyao[root@oldboyedu-50 ~]#
3.3 awk
! 表示取反
[root@oldboyedu-50 ~]# awk '!/oldboy/' /data/test.txt testliyao[root@oldboyedu-50 ~]# awk '/oldboy/' /data/test.txt oldboy[root@oldboyedu-50 ~]#
3.4 sed
d 意思 delete
[root@oldboyedu-50 ~]# sed '/oldboy/d' /data/test.txt testliyao
第四章 显示文件20到30行的内容
准备环境
[root@oldboyedu-50 ~]# seq 40 >/data/ett.txt
4.1 方法一 head | tail
[root@oldboyedu-50 ~]# head -30 /data/ett.txt |tail -112021222324252627282930
4.2 方法二 awk
NR 行号
NR==20 取第三行
[root@oldboyedu-50 ~]# awk 'NR==20,NR==30' /data/ett.txt####因行数太多 此处不写 输出内容同4.1
4.3 方法三 sed
-n 取消默认输出(sed命令不会把文件内容都显示出来)
p (print显示打印)
[root@oldboyedu-50 ~]# sed -n '20,30p' /data/ett.txt
####因行数太多 此处不写 输出内容同4.1