目录
文件管理命令练习
题目如下:
(1)在/opt目录下创建一个临时目录tmp;
先查看是否存在/opt:
[redhat@localhost ~]# ls -d /opt
解答:
[redhat@localhost ~]# mkdir -p /opt/tmp
[redhat@localhost ~]# tree /opt/
(2)在临时目录下创建一个临时文件,文件名为:a.txt;
解答:
[redhat@localhost ~]# touch /opt/tmp/a.txt
[redhat@localhost ~]# ll /opt/tmp/
vi/vim练习
需要完成以下步骤:
(1)应用vi命令在/tmp文件夹下创建文件,文件名newfile。在newfile首行输入日期时间
解答:
先查看/tmp目录是否存在
[redhat@localhost ~]# ls -ld /tmp
drwxrwxrwt. 24 root root 4096 3月 27 19:32 /tmp
创建文件newfile
方法一:【使用vim编辑文件然后直接保存退出(:wq)】
[redhat@localhost ~]# vim /tmp/newfile
[redhat@localhost ~]# ll /tmp/newfile
-rw-r--r--. 1 root root 0 3月 27 19:32 /tmp/newfile
[redhat@localhost ~]# date > /tmp/newfile
[redhat@localhost ~]# cat /tmp/newfile
2023年 03月 27日 星期一 19:10:22 CST
方法二:
[redhat@localhost ~]# touch /tmp/newfile
[redhat@localhost ~]# ll /tmp/newfile
-rw-r--r--. 1 root root 0 3月 27 19:34 /tmp/newfile
[redhat@localhost ~]# date "+%F %T" > /tmp/newfile
[redhat@localhost ~]# cat /tmp/newfile
2023-03-27 19:38:18
(2)将/boot/grub2/grub.cfg文档的内容读入到newfile文档中(在日期的下一行即第二行)
解答:
方法一:
使用vim打开/tmp/newfile,然后输入:r /boot/grub2/grub.cfg,最后输入:wq进行保存退出
[redhat@localhost ~]# vim /tmp/newfile
方法二:
[redhat@localhost ~]# cat /boot/grub2/grub.cfg >> /tmp/newfile
(3)查找文档中包含#号字符的行,将整行删除
解答:
使用vim打开文件输入:
g/#/d
最后再输入:wq 进行保存退出
(4)开启VI的行号提示功能
解答:
使用vim打开文件输入
set nu 便可以查看行号
开启行号提示之后: