<<<第四单元练习>>>
1.在student用户下执行find /etc -name passwd ## 查找etc目录下名字为passwd的文件 命令,并管理其输出要求如下:
* 显示所有正确输出,屏蔽错误输出
find /etc -name passwd 2>/dev/null
## 系统设置,当打开一个bash时,会在/dev/pts/目录下产生一个文件{0..n},每执行一条命令,得到的结果,也就是终端上显示的内容,会保存在这个文件中。当使用 > 时,为重定向输 入保存至某个指定文件
## /dev/null 相当于垃圾箱的作用
## 如果只执行find /etc -name passwd,则输出结果保存在系统默认的文件里
## 2是表示输出错误结果的编号级别为2,输出正确结果的编号级别是1。
## 当执行find /etc -name passwd >/dev/null时,默认编号级别是1,会将正确的输出结果放到垃圾箱里
* 保存正确数出到/mnt/find.out,错误数出到/mnt/find.err中
find /etc -name passwd 2>/mnt/find.err | tee /mnt/find.out
## 完成这个操作,需要建立两个文件/mnt/find.out和/mnt/find.err,而student用户没有在/mnt/目录中建立文件的权限,所以需要切换到root用户,修改/mnt/的权限
## 修改/mnt/的权限
su - root
chmod 777 /mnt/
logout
其中,777表示为/mnt/增加可读可写可执行的权限,也可以用u+g+o
## 建立两个文件 touch /mnt/find.{out,err}
* 建立/mnt/find.all文件,并且保存所有输出到此文件中
touch /mnt/find.all
find /etc -name passwd &>/mnt/find.all
# &> 表示重定向所有输出
* 再次保存所有输出到/mnt/find.all中,并且保持源文件内容
find /etc -name passwd &>>/mnt/find.all
## &>>表示追加在文件末尾重定向所有输出
* 屏蔽此命令的所有输出
find /etc -name passwd &>/dev/null
* 显示此命令的所有输出并保存输出到桌面上的任意文件中
find /etc -name passwd 2>&1 | tee file
## | 管道符,只允许正确输出通过
## | tee 复制一份输出
* 保存正确输出到/mnt/find.out.1中,屏蔽错误输出
touch /mnt/find.out.1
find /etc -name passwd 2>/dev/null | tee /mnt/find.out.1
2.处理文件在文件/usr/share/mime/packages/freedesktop.org.xml要求如下:
* 找到此文件中包含ich的行,并保存这些行到/root/lines中
## 修改/root/权限或用超级用户进行操作
touch /root/lines
grep 'ich' /usr/share/mime/packages/freedesktop.org.xml >/root/lines
## grep 文本搜索工具
* 用vim替换掉/root/lines中的空格,但要保持文件中原有的内容
vim /root/lines
命令模式下输入
:%s/^\ *//g
## 在命令模式中
:%s/原字符/替换后字符/g
## ^表示从头开始
grep