学习老男孩Linux的笔记总结12【第67课】67-不断补充“企业重点案例”用法的笔记

题目一:
已知文件test.txt内容为:
test
liyao
oldboy
请给出打印test.txt内容时,不包含oldboy字符串的命令


方法一:
[root@localhost oldboy]# cat >>test.txt<<EOF
> test
> liyao
> oldboy
> EOF
[root@localhost oldboy]# cat test.txt
test
liyao
oldboy
[root@localhost oldboy]# head -2 test.txt
test
liyao
方法二:
[root@localhost oldboy]# grep -v 'oldboy' test.txt
test
liyao
方法三:
[root@localhost oldboy]# sed '/oldboy/d' test.txt
test
liyao

题目二:

请用一条命令完成创建目录/oldboy/test,即创建/oldboy目录及/oldboy/test目录

[root@localhost /]# mkdir /oldboy/test
mkdir: cannot create directory ‘/oldboy/test’: No such file or directory
[root@localhost /]# mkdir -p /oldboy/test       //-p递归创建
[root@localhost /]# cd /oldboy/test/
[root@localhost test]# pwd
/oldboy/test

 

题目三:

已知/tmp目录下已经存在test.txt文件,如何执行命令才能把/mnt/test.txt拷贝到/tmp下覆盖掉/tmp/test.txt,而让Linux系统不提示是否覆盖(root权限下)

[root@localhost /]# touch /tmp/test.txt
[root@localhost /]# touch /mnt/test.txt
[root@localhost /]# cp /mnt/test.txt /tmp/
cp: overwrite ‘/tmp/test.txt’? y

方法一:使用全路径

[root@localhost /]# cat /tmp/test.txt
This is tmp's test.txt
[root@localhost /]# cat /mnt/test.txt
This is mnt's test.txt
[root@localhost /]# which cp
alias cp='cp -i'
        /usr/bin/cp
[root@localhost /]# /usr/bin/cp /mnt/test.txt tmp/
[root@localhost /]# /bin/cp /mnt/test.txt /tmp     //这个命令和上一条命令相同
[root@localhost /]# cat /tmp/test.txt
This is mnt's test.txt

方法二:用反斜线

[root@localhost /]# cat /tmp/test.txt
This is tmp's test.txt
[root@localhost /]# cat /mnt/test.txt
This is mnt's test.txt
[root@localhost /]# \cp /mnt/test.txt /tmp/
[root@localhost /]# cat /tmp/test.txt
This is mnt's test.txt

题目四:

/oldboy目录及其子目录下所有以.txt结尾的文件中的oldboy字符串替换成oldgirl

[root@localhost oldboy]# mkdir a b c d e
[root@localhost oldboy]# echo oldboy >a/test.txt
[root@localhost oldboy]# echo oldboy >c/test.txt
[root@localhost oldboy]# mkdir -p a/bbb/ffff
[root@localhost oldboy]# echo oldboy >a/bbb/ffff/test.txt
[root@localhost oldboy]# find ./ -type f -name "test.txt"|xargs sed -i 's#oldboy#oldgirl#g'
[root@localhost oldboy]# cat a/test.txt
oldgirl
[root@localhost oldboy]# cat c/test.txt
oldgirl
[root@localhost oldboy]# cat a/bbb/ffff/test.txt
oldgirl


 

posted on 2019-05-21 16:06  Erica程  阅读(117)  评论(0)    收藏  举报

导航