第一章 第6题【管道的概念】进入/root目录下的data目录,删除oldboy.txt文件
1.1 rm 删除 (remove)
例子:
[root@oldboyedu-40 data]# rm oldboy.txt
rm: remove regular empty file `oldboy.txt'? y
[root@oldboyedu-40 data]# ls -l
total 12
-rw-r--r--. 1 root root 0 Aug 11 22:42 oldgirl.txt
-rw-r--r--. 1 root root 28 Aug 11 23:06 tanjiaoshou
-rw-r--r--. 1 root root 28 Aug 11 23:11 tanjiaoshou_bak
-rw-r--r--. 1 root root 28 Aug 11 23:10 tanjiaoshou.bak
1.1.1 -f 强制删除
例子:
[root@oldboyedu-40 data]# rm -f oldgirl.txt
[root@oldboyedu-40 data]# ls -l
total 12
-rw-r--r--. 1 root root 28 Aug 11 23:06 tanjiaoshou
-rw-r--r--. 1 root root 28 Aug 11 23:11 tanjiaoshou_bak
-rw-r--r--. 1 root root 28 Aug 11 23:10 tanjiaoshou.bak
1.1.2 -r 递归删除
例子:
[root@oldboyedu-40 data]# rm -r /root/data/
rm: descend into directory `/root/data'? y
rm: remove regular file `/root/data/tanjiaoshou'? n
rm: remove regular file `/root/data/tanjiaoshou.bak'? n
rm: remove regular file `/root/data/tanjiaoshou_bak'? n
rm: remove directory `/root/data'? n
1.1.3 -rf 递归强制删除
例子:
[root@oldboyedu-40 data]# rm -rf /root/data/
[root@oldboyedu-40 data]# ls -l /root/
total 48
-rw-------. 1 root root 1073 Aug 10 18:40 anaconda-ks.cfg
drwxr-x---. 2 root root 4096 Aug 10 18:50 anaconda-screenshots
-rw-r--r--. 1 root root 21736 Aug 10 18:40 install.log
-rw-r--r--. 1 root root 5890 Aug 10 18:38 install.log.syslog
-rw-r--r--. 1 root root 6 Aug 12 00:33 oldboy.txt
第二章 第7题【管道的概念】进入到/root目录下的data目录,删除oldboy.txt的这个文件
2.1 find 命令使用
示范:
[root@oldboyedu-40 data]# find /data/ -type f -name "oldboy.txt"
/data/oldboy.txt
#find 在哪里找 找什么类型的 找什么名字的
#find /data -type f -name "oldboy.txt"
2.1.1 找出/data目录下面 以.txt结尾的文件
[root@oldboyedu-40 data]# find /data/ -type f -name "*.txt"
/data/oldboy.txt
[root@oldboyedu-40 data]# touch /data/lidao.txt
[root@oldboyedu-40 data]# find /data/ -type f -name "*.txt"
/data/oldboy.txt
/data/lidao.txt
2.1.2 xargs 分组 -n 2 分为俩组 2列
例子:
[root@oldboy40 ~]# xargs -n2 </data/oldboy.txt
1 2
3 4
5
2.1.3 find通过管道和xargs ls -l连接
示范:
find /data -type f -name "*.txt" |xargs ls -l
例子:
[root@oldboy40 ~]# find /data -type f -name "*.txt"|xargs ls -l
-rw-r--r--. 1 root root 0 Aug 15 21:18 /data/oldboy.txt
2.2找出/data目录下面 以.txt结尾的文件
示范:
[root@oldboyedu-40 data]# find /data/ -type f -name "*.txt"
例子:
[root@oldboyedu-40 data]# touch /data/lidao.txt
[root@oldboyedu-40 data]# find /data/ -type f -name "*.txt"
/data/oldboy.txt
/data/lidao.txt
第三章 第8题 请给出输出test.txt文件内容时,不包含oldboy字符串的命令。
已知文件test.txt内容
mkdir /data
cat >>/data/test.txt<<EOF
test
liyao
oldboy
EOF
[root@oldboyedu-40 ~]# cat /data/test.txt
test
liyao
oldboy
3.1方法1-grep 过滤 -v 排除
例子:
[root@oldboyedu-40 ~]# grep "oldboy" /data/test.txt
oldboy
[root@oldboyedu-40 ~]# grep "oldboy " /data/test.txt
[root@oldboyedu-40 ~]# grep -v "oldboy" /data/test.txt
test
liyao
3.2方法2-head用来显示档案的开头至标准输出中 取出文件的前几行 默认取出文件的前10行
[root@oldboyedu-40 ~]# head /data/test.txt
test
liyao
oldboy
[root@oldboyedu-40 ~]# head -n2 /data/test.txt
test
liyao
[root@oldboyedu-40 ~]# head -2 /data/test.txt
test
liyao
[root@oldboyedu-40 ~]# tail -1 /data/test.txt
oldboy
3.2.1 -n<行数> 显示的行数
例子:
[root@oldboyedu-40 ~]# head -n2 /data/test.txt
test
liyao
3.3方法3-sed
[root@oldboyedu-40 ~]# sed '/oldboy/d' /data/test.txt
test
liyao
3.4方法4-awk
[root@oldboyedu-40 ~]# awk '/oldboy/' /data/test.txt
oldboy
[root@oldboyedu-40 ~]# awk '!/oldboy/' /data/test.txt
test
liyao
第四章 第9题 请用一条命令完成创建目录/oldboy/test,即创建/oldboy目录及/oldboy/test
4.1 mkdir -p可以创建多层目录
例子:
[root@oldboyedu-40 ~]# ls -l /oldboy/test
ls: cannot access /oldboy/test: No such file or directory
[root@oldboyedu-40 ~]# ls -l /oldboy/
ls: cannot access /oldboy/: No such file or directory
[root@oldboyedu-40 ~]# ls -l /
[root@oldboyedu-40 ~]# mkdir /oldboy/test
mkdir: cannot create directory `/oldboy/test': No such file or directory
[root@oldboyedu-40 ~]# mkdir -p /oldboy/test
[root@oldboyedu-40 ~]# ls -l /oldboy/test/
total 0
[root@oldboyedu-40 ~]# mkdir -pv /oldboy/test/a/b/c/d/e/f/g/g/h
[root@oldboyedu-40 ~]# mkdir -pv /oldboy/test/a/b/c/d/e/f/g/g/h
mkdir: created directory `/oldboy/test/a'
mkdir: created directory `/oldboy/test/a/b'
mkdir: created directory `/oldboy/test/a/b/c'
mkdir: created directory `/oldboy/test/a/b/c/d'
mkdir: created directory `/oldboy/test/a/b/c/d/e'
mkdir: created directory `/oldboy/test/a/b/c/d/e/f'
mkdir: created directory `/oldboy/test/a/b/c/d/e/f/g'
mkdir: created directory `/oldboy/test/a/b/c/d/e/f/g/g'
mkdir: created directory `/oldboy/test/a/b/c/d/e/f/g/g/h'
第五章 重定向符号如何使用
>或1> 标准输出重定向 先把原文件内容清空,然后在把新的内容放到文件中
6.2 >>或1>>追加输出重定向 把前面输出的东西输入到后边的文件中,不会清除文件原有内容,只是追加到文件的最后一行
例子:
[root@oldboyedu-40 ~]# echo oldboyedu.com
>>oldboy.txt
[root@oldboyedu-40 ~]# echo oldboyedu.com
>>oldboy.txt
[root@oldboyedu-40 ~]# echo oldboyedu.com
>>oldboy.txt
[root@oldboyedu-40 ~]# cat oldboy.txt
oldboyedu.com
oldboyedu.com
oldboyedu.com
[root@oldboyedu-40 ~]# echo oldboyedu.cn
>oldboy.txt
[root@oldboyedu-40 ~]# cat oldboy.txt
oldboyedu.cn
6.3 2>> 错误追加重定向 把命令执行错误的信息追加存放在文件中
6.4 2> 错误输出重定向 先把原文件内容清空,然后在把新的内容放到文件中
如何把出错的信息也放到文件里
示范:
echa oldboyedu.com 2>>oldboy.txt
例子:
[root@oldboyedu-40 ~]# echa oldboyedu.com
>>oldboy.txt
-bash: echa: command not found
[root@oldboyedu-40 ~]# cat oldboy.txt
oldboyedu.cn
oldboyedu.xyz
[root@oldboyedu-40 ~]# # >> > 把正确的内容放到文件中.
[root@oldboyedu-40 ~]# #正确 错误 是否能正常运行命令
[root@oldboyedu-40 ~]#
[root@oldboyedu-40 ~]# echa oldboyedu.com
-bash: echa: command not found
[root@oldboyedu-40 ~]# echa oldboyedu.com
2>>oldboy.txt
[root@oldboyedu-40 ~]# echa oldboyedu.com
2>>oldboy.txt
[root@oldboyedu-40 ~]# echa oldboyedu.com
2>>oldboy.txt
[root@oldboyedu-40 ~]# cat oldboy.txt
oldboyedu.cn
oldboyedu.xyz
-bash: echa: command not found
-bash: echa: command not found
-bash: echa: command not found
如何把错误的和正确的信息都放到文件里
1.
[root@oldboyedu-40 ~]# echo oldboyedu.com
>>oldboy.txt 2>>oldboy.txt
[root@oldboyedu-40 ~]# cat oldboy.txt
oldboyedu.cn
oldboyedu.xyz
-bash: echa: command not found
-bash: echa: command not found
-bash: echa: command not found
oldboyedu.com
2.
[root@oldboyedu-40 ~]# cho oldboyedu.com
>>oldboy.txt 2>>oldboy.txt
[root@oldboyedu-40 ~]# cat oldboy.txt
oldboyedu.cn
oldboyedu.xyz
-bash: echa: command not found
-bash: echa: command not found
-bash: echa: command not found
oldboyedu.com
-bash: cho: command not found
3
[root@oldboy40 ~]# echo oldboyedu.com >>oldboy.txt 2>>oldboy.txt
[root@oldboy40 ~]# cat oldboy.txt
oldboyedu.com
oldboyedu.com
[root@oldboy40 ~]# echo oldboyedu.com >>oldboy.txt 2>&1
[root@oldboy40 ~]# cat oldboy.txt
oldboyedu.com
oldboyedu.com
oldboyedu.com
[root@oldboy40 ~]#
4
[root@oldboyedu-40 ~]# cho oldboyedu.com
>>oldboy.txt 2>&1
[root@oldboyedu-40 ~]# echo oldboyedu.com
>>oldboy.txt 2>&1
[root@oldboyedu-40 ~]# cat oldboy.txt
-bash: cho: command not found
oldboyedu.com