命令行管理文件:
1、创建文件命令练习:
(1) 在/目录下创建一个临时目录test;
(2)在临时目录test下创建五个文件,文件名分别为:passwd,group,bashrc,profile,sshd_config;
(3)在/test创建/etc/motd的软链接,文件名为motd.soft;创建/etc/motd的硬链接为motd.hard;
[root@localhost /]# mkdir /test #创建临时目录test
[root@localhost /]# cd test/ #改变目录位置,即变化到test临时目录下
[root@localhost test]# touch {passwd,group,bashrc,profile,sshd_config} #在临时目录下创建五个文件
[root@localhost test]# ln -s /etc/motd motd.soft #依题目要求创建软链接
[root@localhost test]# ln /etc/motd motd.hard #依题目要求创建硬链接
2、重定向练习:
(1)将系统内核版本信息,发行版本信息,写入到/test/motd.soft文件中
(2)将当前主机主机名,当前用户使用的shell信息追加到/test/motd.hard文件中
(3)将根目录下的文件的文件名写入/test/file文件中
(4)查看当前工作目录是否为/test目录,将当前工作目录的详细信息追加到/test/file文件中
[root@localhost ~]# uname -r >> test/motd.soft #uname -r表示内核信息,追加到指定文件下
[root@localhost test]# cat /etc/redhat-release >> motd.soft #版本信息
[root@localhost ~]# cat /etc/hostname >> /test/motd.hard #当前主机主机名进行追加
[root@localhost ~]# echo $SHELL >> test/motd.hard #使用shell信息追加到指定文件
[root@localhost test]# ls / >> file #将根目录的文件写入指定文件中
[root@localhost test]# pwd #查看当前目录是否为/test目录
[root@localhost ~]# ls -R test/* >> test/file #运用递归操作,将test详细信息追加到指定文件
3、tee命令练习:
(1)将当前时间添加至/test目录下的passwd,group,bashrc,profile,sshd_config文件中
(2)将当前用户的用户名追加至/test目录下的passwd,group,bashrc,profile,sshd_config文件中
[root@localhost test]# date | tee /test/{passwd,group,bashrc,profile,sshd_config}
Fri Oct 21 08:46:34 PM CST 2022
#结合管道符,将时间添加到相对应文件中
[root@localhost test]# hostname | tee -a /test/{passwd,group,bashrc,profile,sshd_config}
#结合管道符,将用户名追加到指定目录下的文件中,此处要加-a进行追加
4、vim命令练习:
(1)将/etc/passwd文件内容读入/test/passwd,并修改文件里的root字符为admin
(2)将/etc/group文件内容读入/test/group,只保留root开头的行内容
(3)将/root/.bashrc文件内容读入/test/bashrc,删除#号开头的行内容
(4)将/etc/ssh/sshd_config文件内容读入/test/sshd_config,在该文件的第17行后添加一行内容Port 22
(5)将/test/sshd_config文件中的第40-50行的yes改为no
(6)将/test/sshd_config文件另存为/test/sshd.conf
(7)将/test目录下的passwd,group,bashrc文件中的第一行内容复制至文档最后一行
(8)将/test目录下的profile,sshd_config文件中前两行内容复制至文档倒数第二行
(1)
[root@localhost test]# cat /etc/passwd > /test/passwd #将/etc/passwd/文件内容读入指定位置
[root@localhost test]# vim /etc/passwd #进入vi编辑器中,输入如下命令:
:s/root/admin/g
(2)
[root@localhost test]# cat /etc/group > /test/group
[root@localhost test]# vim /test/group
:v/^root/d #v表示找到列,^root:定位到以root开头的文件,d:删除操作
(3)
[admin@localhost test]# cat /root/.bashrc > /test/bashrc
[admin@localhost test]# vim /test/bashrc
:^# d
(4)
[admin@localhost test]# cat /etc/ssh/sshd_config > /test/sshd_config
[admin@localhost test]# vim /test/sshd_config
:17gg #进入vim界面后,在键盘上敲17gg,即可定位到第17行
Port 22 #并在其后面输入,Port 22
(5)
[admin@localhost test]# vim /test/sshd_config
:40,50 s/yes/no/g
(6)
[admin@localhost test]# vim /test/sshd_config
:w /test/ssh.conf #另存为
(7)
[admin@localhost test]# vim /test/passwd
#进入视图后,在键盘上输入gg(定位到首行)。然后在键盘上yy(复制光标所在的行),紧接着在输入G(定位到最后一行)。最后,输入p(小写,粘贴到所在行的下一行)
(8)
[admin@localhost test]# vim /test/profile
#进入视图后,在键盘上输入gg(定位到首行),输入2yy(复制前两行),紧接着输入G(定位到最后一行)。最后,输入P(大写,粘贴到所在行的上一行)