1.创建文件命令练习
1、创建文件命令练习:
(1) 在/目录下创建一个临时目录test;
root@server ~]# mkdir /test
(2)在临时目录test下创建五个文件,文件名分别为passwd,group,bashrc,profile,
sshd_config;
[root@server test]# touch /test/{passwd,group,bashrc,profile,sshd_config}
[root@server test]# ll
total 8
-rw-r--r--. 1 root root 0 Oct 20 10:52 bashrc
-rw-r--r--. 1 root root 0 Oct 20 10:52 group
-rw-r--r--. 1 root root 0 Oct 20 10:52 passwd
-rw-r--r--. 1 root root 0 Oct 20 10:52 profile
-rw-r--r--. 1 root root 0 Oct 20 10:52 sshd_config
(3)在/test创建/etc/motd的软链接,文件名为motd.soft;创建/etc/motd的硬链接为motd.hard
[root@server ~]# ln -s /etc/motd /test/motd.soft && ln /etc/motd /test/motd.hard
[root@server ~]# ll /test/m*
-rw-r--r--. 2 root root 0 Sep 10 2018 /test/motd.hard
lrwxrwxrwx. 1 root root 9 Oct 20 10:34 /test/motd.soft -> /etc/motd
2、重定向练习:
(1)将系统内核版本信息,发行版本信息,写入到/test/motd.soft文件中
[root@server ~]# cat /etc/redhat-release >> /test/motd.soft && uname -r >> /test/motd.soft
[root@server ~]# cat /test/motd.soft
Red Hat Enterprise Linux release 8.5 (Ootpa)
4.18.0-348.el8.x86_64
(2)将当前主机主机名,当前用户使用的shell信息追加到/test/motd.hard文件中
[root@server ~]# hostname >> /test/motd.hard && echo $SHELL >> /test/motd.hard
[root@server ~]# cat /test/motd.hard
Red Hat Enterprise Linux release 8.5 (Ootpa)
4.18.0-348.el8.x86_64
server.local
/bin/bash
(3)将根目录下的文件的文件名写入/test/file文件中
[root@server ~]# ls / > /test/file
[root@server ~]# cat /test/file
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
test
tmp
usr
var
(4)查看当前工作目录是否为/test目录,将当前工作目录的详细信息追加到/test/file文件中
[root@server test]# pwd
/test
[root@server test]# stat /test >> /test/file
[root@server test]# cat /test/file
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
test
tmp
usr
var
File: /test
Size: 127 Blocks: 0 IO Block: 4096 directory
Device: 10303h/66307d Inode: 101692654 Links: 7
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2022-10-20 10:34:44.559881191 -0400
Modify: 2022-10-20 10:43:44.332945941 -0400
Change: 2022-10-20 10:43:44.332945941 -0400
Birth: 2022-10-20 10:30:21.085446684 -0400
3、tee命令练习:
(1)将当前时间添加至/test目录下的passwd,group,bashrc,profile,sshd_config文件中
[root@server test]# date +%F-%S | tee passwd group bashrc profile sshd_config
2022-10-21-19:36:00
[root@server test]# cat passwd group bashrc profile sshd_config
2022-10-21-19:36:00
2022-10-21-19:36:00
2022-10-21-19:36:00
2022-10-21-19:36:00
2022-10-21-19:36:00
2022-10-21-19:36:00
(2)将当前用户的用户名追加至/test目录下的passwd,group,bashrc,profile,sshd_config文件
中
[root@server test]# whoami | tee -a passwd group bashrc profile sshd_config
root
[root@server test]# cat passwd group bashrc profile sshd_config
2022-10-21-19:36:00
root
2022-10-21-19:36:00
root
2022-10-21-19:36:00
root
2022-10-21-19:36:00
root
2022-10-21-19:36:00
root
4、vim命令练习:
(1)将/etc/passwd文件内容读入/test/passwd,并修改文件里的root字符为admin
进入 vim /test/passwd
进入末行模式,按 : 进入,然后 r /etc/passwd
输入%s#root#admin#g
(2)将/etc/group文件内容读入/test/group,只保留root开头的行内容
进入 vim /test/group
进入末行模式,按 : 进入,然后 r /etc/group
v/^root/d
(3)将/root/.bashrc文件内容读入/test/bashrc,删除#号开头的行内
进入 vim /test/bashrc
进入末行模式,按 : 进入,然后 r /root/.bashrc
%/^#//g
(4)将/etc/ssh/sshd_config文件内容读入/test/sshd_config,在该文件的第17行后添加一行内容
Port 22
进入 vim /test/sshd_config
进入末行模式,按 : 进入,然后 r /etc/ssh/sshd_config
:set nu
:17
按ESC 点击 o键
输入 Port 22
(5)将/test/sshd_config文件中的第40-50行的yes改为no
:40,50s#yes#no#g
(6)将/test/sshd_config文件另存为/test/sshd.conf
:w /test/sshd.conf
:wq
[root@server test]# ll
total 40
-rw-r--r--. 1 root root 199 Oct 21 07:56 bashrc
-rw-r--r--. 1 root root 0 Oct 20 11:37 date
-rw-r--r--. 1 root root 512 Oct 20 10:46 file
-rw-r--r--. 1 root root 35 Oct 21 07:51 group
-rw-r--r--. 2 root root 90 Oct 20 10:42 motd.hard
lrwxrwxrwx. 1 root root 9 Oct 20 10:34 motd.soft -> /etc/motd
-rw-r--r--. 1 root root 2575 Oct 21 07:45 passwd
-rw-r--r--. 1 root root 25 Oct 21 07:37 profile
-rw-r--r--. 1 root root 4299 Oct 21 08:03 sshd.conf
-rw-r--r--. 1 root root 4299 Oct 21 08:03 sshd_config
(7)将/test目录下的passwd,group,bashrc文件中的第一行内容复制至文档最后一行
进入 vim /test/passwd
1 co $
(8)将/test目录下的profile,sshd_config文件中前两行内容复制至文档倒数第二行
进入 vim /test/profile
1,2 co $-1,$