首页 > 代码库 > 老男孩Linux-初级第36期入学前考试题解答-吕晓雪

老男孩Linux-初级第36期入学前考试题解答-吕晓雪


[考试试题]

1.创建一个目录/data

解答:

[root@oldboy ~]# mkdir /data
[root@oldboy ~]# ls /
bin  dev   lib         media  opt   sbin     sys var
boot etc   lib64       mnt    proc  selinux  tmp xue
data  home  lost+found oldboy  root  srv     usr  xuexue

 

2./data下面建立一个文件oldboy.txt

解答:

[root@oldboy ~]# touch /data/oldboy.txt
[root@oldboy ~]# ls /data
oldboy.txt


 

3.oldboy.txt增加内容为I amstudying linux.

解答:

方法(1

[root@oldboy ~]# vi /data/oldboy.txt

I amstudying linux                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

-- INSERT --

:wq 保存

 

方法(2

[root@oldboy ~]# echo ‘I am studying linux‘ >/data/oldboy.txt

[root@oldboy ~]# cat /data/oldboy.txt

I amstudying linux

 

 

 

 

方法(3

[root@oldboy data]# cat >>oldboy.txt<<qq

> I am studying linux

> qq

[root@oldboy data]# cat oldboy.txt

I amstudying linux

 

4.oldboy.txt文件拷贝到/tmp

解答:

[root@oldboy ~]# cp /data/oldboy.txt /tmp/

[root@oldboy ~]# ls /tmp

oldboy.txt   

 

5./data目录移动到/root

[root@oldboy~]# mv /data /root

[root@oldboy~]# ls

anaconda-ks.cfg  install.log.syslog  nohup.out

data           

 

6.进入/root目录下的data目录,删除oldboy.txt文件

解答:

方法(1)

[root@oldboy ~]# rm -f data/oldboy.txt

[root@oldboy ~]# ls data

[root@oldboy ~]# 

方法(2

[root@oldboy ~]# find data -type f -name‘oldboy.txt‘

data/oldboy.txt

[root@oldboy ~]# find data -type f -name ‘oldboy.txt‘|xargs rm -f

[root@oldboy ~]# ls data/

[root@oldboy ~]#

 

方法(3

[root@oldboy ~]# find data -type f -name ‘oldboy.txt‘ -exec rm {} \;

[root@oldboy ~]# ls data/

[root@oldboy ~]#

 

7.接第6题,退出到上一级目录,删除data目录。 

方法(1

[root@oldboy ~]# cd ..

[root@oldboy /]# rm -fr root/data/

[root@oldboy /]# ls root

anaconda-ks.cfg install.log.syslog nohup.out test  

方法(2

[root@oldboy /]# find root -type d -name ‘data‘

root/data

[root@oldboy /]# find root -type d -name ‘data‘|xargs rm -fr

[root@oldboy /]# ls root

anaconda-ks.cfg  install.log.syslog nohup.out test

 

 8.已知文件 test.txt 内容为:

test

liyao

oldboy

请给出输出 test.txt文件内容时,不包含oldboy 字符串的命令。

解答:

模拟环境

[root@oldboy data]# cat>>test.txt<<qq

> test

> liyao

> oldboy

> qq

[root@oldboy data]# cat test.txt

test

liyao

oldboy

方法(1

[root@oldboy data]# grep -v oldboy test.txt

test

liyao

 

方法(2

[root@oldboy data]# sed ‘/oldboy/d‘ test.txt

test

liyao

 

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

解答:

[root@oldboy data]# mkdir -p oldboy/test

[root@oldboy data]# ls

oldboy

[root@oldboy data]# ls oldboy/

test

 

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

解答:

[root@oldboy /]# cp /mnt/test.txt /tmp

cp:是否覆盖"/tmp/test.txt"? ^C

方法(1

[root@oldboy /]# \cp /mnt/test.txt /tmp

方法(2

[root@oldboy /]# bin/cp /mnt/test.txt /tmp   

   

 

 

 

 

11.只查看ett.txt文件(共100行)内第20到第30行的内容

解答:

模拟环境:

[root@oldboy data]# seq 100 >ett.txt

[root@oldboy data]# cat ett.txt

1

......

100

 

方法(1

[root@oldboy data]# sed -n ‘20,30‘p ett.txt

20

......

30

 

方法(2

[root@oldboy data]# grep 20 -A 10 ett.txt

20

.....

30

方法 (3)

[root@oldboy data]# grep 30 -B 10 ett.txt

20

......

30

方法(4

[root@oldboy data]# grep 25 -C 5 ett.txt

20

......

30

方法(5

[root@oldboy data]# awk ‘NR>19&&NR<31‘ ett.txt

 

方法(6非自己

[root@oldboy data]# awk ‘{if(NR<31 && NR>19) printf$0"\n"}‘ ett.txt

20

......

30

 

方法(7

[root@oldboy data]# head -30 ett.txt | tail -11

20

......

30

 

 

13、把/oldboy目录及其子目录下所有以扩展名.sh结尾的文件中包含./hostlists.txt的字符串全部替换为../idctest_iplist。【附加题:加分题,不会的同学可以放弃不答】。

解答:

模拟环境:

[root@oldboy ~]# mkdir -p oldboy/oldgil

[root@oldboy ~]# cd oldboy

[root@oldboy oldboy]# echo ‘./hostlists.txt‘>a.sh

[root@oldboy oldboy]# echo ‘./hostlists.txt‘>b.sh

[root@oldboy oldboy]# echo ‘./hostlists.txt‘>c.sh

[root@oldboy oldboy]# echo ‘./hostlists.txt‘>e.txt

[root@oldboy oldboy]# echo ‘./hostlists.txt‘>f.txt

[root@oldboy oldboy]# cd oldgil/

[root@oldboy oldgil]# echo ‘./hostlists.txt‘>g.txt

[root@oldboy oldgil]# echo ‘./hostlists.txt‘>h.sh

[root@oldboy oldgil]# echo ‘./hostlists.txt‘>j.sh

[root@oldboy oldgil]# find -type f -name‘*.sh‘|xargs cat

./hostlists.txt

./hostlists.txt

[root@oldboy oldgil]# cd ..

[root@oldboy oldboy]# tree

.

├── a.sh

├── b.sh

├── c.sh

├── e.txt

├── f.txt

└── oldgil

    ├── g.txt

    ├── h.sh

    └── j.sh

方法:

[root@oldboy oldboy]# find -type f -name ‘*.sh‘|xargs cat

./hostlists.txt

./hostlists.txt

./hostlists.txt

./hostlists.txt

./hostlists.txt

 [root@oldboyoldboy]# find -type f-name ‘*.sh‘|xargs sed ‘s#./hostlists.txt#../idctest_iplist#g‘

oldboy

./hostlists.ext

./hostlists.ext

./hostlists.ext

./hostlists.ext

../idctest_iplist

../idctest_iplist

../idctest_iplist

../idctest_iplist

../idctest_iplist

[root@oldboy ~]# find -type f -name ‘*.sh‘|xargs sed -i ‘s#./hostlists.txt#../idctest_iplist#g‘

[root@oldboy ~]# find oldboy -type f -name‘*.sh‘|xargs cat

../idctest_iplist

../idctest_iplist

../idctest_iplist

../idctest_iplist

../idctest_iplist

 

 

 


老男孩Linux-初级第36期入学前考试题解答-吕晓雪