一、作业题目
一.重定向练习题
1. 将命令(比如,ls )的标准输出重定向到文件`output.txt`。
[root@root ~]# ls > output.txt
2. 将命令(比如,ls /aaa)的标准错误重定向到文件`error.txt`。
[root@root ~]# ls /aaa 2> error.txt
3. 将命令 (比如,ls ~ /aaa)的标准输出和标准错误都重定向到文件`output_and_error.txt`。
[root@root ~]# ls ~ /aaa > output_and_error.txt 2>&1
4. 将命令(比如,ls /aaa)的输出追加到文件`output.txt`(而不是覆盖文件)。
[root@root ~]# ls /aaa >> output.txt
5. 将文件`input.txt`作为cat的标准输入。
[root@root ~]# cat < input.txt
6. 将命令1的执行结果通过管道符传递给命令2。
[root@root ~]# ls | grep "a"
7. 将ls的输出同时显示在终端和保存到文件`output.txt`。
[root@root ~]# ls | tee output.txt
8. 将ls的输出重定向到`/dev/null`,以丢弃输出。
[root@root ~]# ls > /dev/null
9. 将两个文件合并输出到一个新文件里面(cat命令)
[root@root ~]# cat file1.txt file2.txt >> output.txt
10. 创建三个文件,内容随意,将这三个文件的内容显示在屏幕上并且合并为一个新的文件。
#创建三个文件
[root@root ~]# echo "Content of file1" > file1.txt
[root@root ~]# echo "Content of file2" > file2.txt
[root@root ~]# echo "Content of file3" > file3.txt
#将三个文件内容显示到屏幕上
[root@root ~]# cat file1.txt file2.txt file3.txt
#将三个文件内容合并为一个新文件
[root@root ~]# cat file1.txt file2.txt file3.txt > merged.txt
二.vim练习题
1. 将/etc/passwd复制到当前用户的家目录(以下的操作都是对~/passwd进行编辑)
[root@root ~]# cp /etc/passwd ~/passwd
[root@root ~]# vim ~/passwd
2. 将第一行中的root全部替换为toor
:1s/root/toor/g
3. 替换1-10行所有的冒号为分号
:1,10s/:/;/g
4. 全文替换x为z
:%s/x/z/g
5. 将光标移动到第10行删除之后的2行
:10, +2d
6. 删除10行-20行的内容
:10,20d
7. 复制第10-20的内容并粘贴到20行之后
:10,20t20
8. 删除第2行的第1个冒号后面的内容
:2s/:.*//
9. 删除第3行的第一个冒号前面的内容
:3s/.*:/:/