[考试试题]
1.创建一个目录/data
解答:
[[email protected] ~]# mkdir /data [[email protected] ~]# 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
解答:
[[email protected] ~]# touch /data/oldboy.txt [[email protected] ~]# ls /data oldboy.txt
3.为oldboy.txt增加内容为“I amstudying linux.
解答:
方法(1)
[[email protected] ~]# vi /data/oldboy.txt
I amstudying linux
-- INSERT --
:wq 保存
方法(2)
[[email protected] ~]# echo ‘I am studying linux‘ >/data/oldboy.txt
[[email protected] ~]# cat /data/oldboy.txt
I amstudying linux
方法(3)
[[email protected] data]# cat >>oldboy.txt<<qq
> I am studying linux
[[email protected] data]# cat oldboy.txt
I amstudying linux
4.把oldboy.txt文件拷贝到/tmp下
解答:
[[email protected] ~]# cp /data/oldboy.txt /tmp/
[[email protected] ~]# ls /tmp
oldboy.txt
5.把/data目录移动到/root下
[[email protected]~]# mv /data /root
[[email protected]~]# ls
anaconda-ks.cfg install.log.syslog nohup.out
data
6.进入/root目录下的data目录,删除oldboy.txt文件
解答:
方法(1)
[[email protected] ~]# rm -f data/oldboy.txt
[[email protected] ~]# ls data
[[email protected] ~]#
方法(2)
[[email protected] ~]# find data -type f -name‘oldboy.txt‘
data/oldboy.txt
[[email protected] ~]# find data -type f -name ‘oldboy.txt‘|xargs rm -f
[[email protected] ~]# ls data/
[[email protected] ~]#
方法(3)
[[email protected] ~]# find data -type f -name ‘oldboy.txt‘ -exec rm {} \;
[[email protected] ~]# ls data/
[[email protected] ~]#
7.接第6题,退出到上一级目录,删除data目录。
方法(1)
[[email protected] ~]# cd ..
[[email protected] /]# rm -fr root/data/
[[email protected] /]# ls root
anaconda-ks.cfg install.log.syslog nohup.out test
方法(2)
[[email protected] /]# find root -type d -name ‘data‘
root/data
[[email protected] /]# find root -type d -name ‘data‘|xargs rm -fr
[[email protected] /]# ls root
anaconda-ks.cfg install.log.syslog nohup.out test
8.已知文件 test.txt 内容为:
test
liyao
oldboy
请给出输出 test.txt文件内容时,不包含oldboy 字符串的命令。
解答:
模拟环境:
[[email protected] data]# cat>>test.txt<<qq
> test
> liyao
> oldboy
[[email protected] data]# cat test.txt
test
liyao
oldboy
方法(1)
[[email protected] data]# grep -v oldboy test.txt
test
liyao
方法(2)
[[email protected] data]# sed ‘/oldboy/d‘ test.txt
test
liyao
9.请用一条命令完成创建目录/oldboy/test,即创建/oldboy目录及/oldboy/test目录
解答:
[[email protected] data]# mkdir -p oldboy/test
[[email protected] data]# ls
oldboy
[[email protected] data]# ls oldboy/
test
10.已知/tmp 下已经存在 test.txt 文件,如何执行命令才能把/mnt/test.txt 拷贝到/tmp 下覆盖掉 /tmp/test.txt,而让系统不提示是否覆盖(root 权限下)。
解答:
[[email protected] /]# cp /mnt/test.txt /tmp
cp:是否覆盖"/tmp/test.txt"? ^C
方法(1)
[[email protected] /]# \cp /mnt/test.txt /tmp
方法(2)
[[email protected] /]# bin/cp /mnt/test.txt /tmp
11.只查看ett.txt文件(共100行)内第20到第30行的内容
解答:
模拟环境:
[[email protected] data]# seq 100 >ett.txt
[[email protected] data]# cat ett.txt
1
......
100
方法(1)
[[email protected] data]# sed -n ‘20,30‘p ett.txt
20
......
30
方法(2)
[[email protected] data]# grep 20 -A 10 ett.txt
20
.....
30
方法 (3)
[[email protected] data]# grep 30 -B 10 ett.txt
20
......
30
方法(4)
[[email protected] data]# grep 25 -C 5 ett.txt
20
......
30
方法(5)
[[email protected] data]# awk ‘NR>19&&NR<31‘ ett.txt
方法(6)非自己
[[email protected] data]# awk ‘{if(NR<31 && NR>19) printf$0"\n"}‘ ett.txt
20
......
30
方法(7)
[[email protected] data]# head -30 ett.txt | tail -11
20
......
30
13、把/oldboy目录及其子目录下所有以扩展名.sh结尾的文件中包含./hostlists.txt的字符串全部替换为../idctest_iplist。【附加题:加分题,不会的同学可以放弃不答】。
解答:
模拟环境:
[[email protected] ~]# mkdir -p oldboy/oldgil
[[email protected] ~]# cd oldboy
[[email protected] oldboy]# echo ‘./hostlists.txt‘>a.sh
[[email protected] oldboy]# echo ‘./hostlists.txt‘>b.sh
[[email protected] oldboy]# echo ‘./hostlists.txt‘>c.sh
[[email protected] oldboy]# echo ‘./hostlists.txt‘>e.txt
[[email protected] oldboy]# echo ‘./hostlists.txt‘>f.txt
[[email protected] oldboy]# cd oldgil/
[[email protected] oldgil]# echo ‘./hostlists.txt‘>g.txt
[[email protected] oldgil]# echo ‘./hostlists.txt‘>h.sh
[[email protected] oldgil]# echo ‘./hostlists.txt‘>j.sh
[[email protected] oldgil]# find -type f -name‘*.sh‘|xargs cat
./hostlists.txt
./hostlists.txt
[[email protected] oldgil]# cd ..
[[email protected] oldboy]# tree
.
├── a.sh
├── b.sh
├── c.sh
├── e.txt
├── f.txt
└── oldgil
├── g.txt
├── h.sh
└── j.sh
方法:
[[email protected] oldboy]# find -type f -name ‘*.sh‘|xargs cat
./hostlists.txt
./hostlists.txt
./hostlists.txt
./hostlists.txt
./hostlists.txt
[[email protected]]# 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
[[email protected] ~]# find -type f -name ‘*.sh‘|xargs sed -i ‘s#./hostlists.txt#../idctest_iplist#g‘
[[email protected] ~]# find oldboy -type f -name‘*.sh‘|xargs cat
../idctest_iplist
../idctest_iplist
../idctest_iplist
../idctest_iplist
../idctest_iplist