• Linux命令行管理文件(练习题)


    命令行管理文件:

    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					#依题目要求创建硬链接
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    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详细信息追加到指定文件
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

    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进行追加
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    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(大写,粘贴到所在行的上一行)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    为什么 ArrayList的 初始容量为10?每次扩容1.5倍?
    Java SPI 和 API,傻傻分不清?
    HDFS免重启挂载新磁盘
    WaitGroup原理分析
    【毕业设计】45-基于单片机的智能温度/超温报警计的系统设计(原理图工程+仿真工程+源代码+答辩论文+答辩PPT)
    C++设计模式之Strategy策略模式
    Android 组件化 组件上下依赖关系实现
    如何快速从零开始搭建一个前端项目
    变量及函数命名经验
    514. 自由之路
  • 原文地址:https://blog.csdn.net/weixin_56105279/article/details/127448928