Linux简单练习(二)

文章详细介绍了在Linux环境中如何使用head和tail命令查看文件特定行,find指令在/etc及其子目录中查找以host开头的文件,grep命令查找指定字符串并重定向输出,以及tar命令打包和gzip压缩文件。此外,还涉及了用户和组管理,包括创建新组和用户,以及使用chown改变文件所有权和所属组。
摘要由CSDN通过智能技术生成

1、文件查看:查看/opt/passwd文件的第6行(使用head和tail指令)

(1)因为passwd在/etc/下面,所以先将其复制到/opt/passwd:

[root@localhost ~]# cp /etc/passwd  /opt/passwd
[root@localhost ~]# ll /opt
总用量 4
-rw-r--r--. 1 root root 2095  4月 10 08:27 passwd

(2)使用命令查看该文件夹前6行内容:

[root@localhost ~]# head -6 /opt/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

 (3)使用管道符将该结果赋给tail命令,即:

[root@localhost ~]# head -6 /opt/passwd |tail -1
sync:x:5:0:sync:/sbin:/bin/sync

完成。
2、在/etc及其子目录中,查找host开头的文件(使用find指令)

使用find命令:

[root@localhost ~]# find /etc -name "host*" -print
/etc/host.conf
/etc/hosts
/etc/avahi/hosts
/etc/nvme/hostnqn
/etc/nvme/hostid
/etc/hostname

完成。
3、查找文件 /usr/share/rhel.xml 中包含字符串 re 的所有行。将所有这些行的信息放在文件/root/files 中(使用grep指令和重定向符号>)

(1)使用grep命令找到需要行(因为没有找到该文件,使用/usr/share/osinfo/os/redhat.com/rhel-9.1.xml代替):

[root@localhost ~]# grep re /usr/share/osinfo/os/redhat.com/rhel-9.1.xml
  <os id="http://redhat.com/rhel/9.1">
    <upgrades id="http://redhat.com/rhel/9.0"/>
    <derives-from id="http://redhat.com/rhel/9.0"/>
    <release-status>prerelease</release-status>
    <!-- RHEL 9.1 entries have to cope both with 9.1 and 9.1.x regexes for
         medias' volume-id and trees' version -->
    <tree arch="aarch64">
      <treeinfo>
      </treeinfo>
    </tree>
    <tree arch="ppc64le">
      <treeinfo>
      </treeinfo>
    </tree>
    <tree arch="s390x">
      <treeinfo>
      </treeinfo>
    </tree>
    <tree arch="x86_64">
      <treeinfo>
      </treeinfo>
    </tree>
    <resources arch="ppc64le">
      <recommended>
      </recommended>
    </resources>
    <resources arch="x86_64">
      <recommended>
      </recommended>
    </resources>
    <resources arch="s390x">
      <recommended>
      </recommended>
    </resources>
    <resources arch="aarch64">
      <recommended>
      </recommended>
    </resources>
    <resources arch="all">
      <recommended>
      </recommended>
    </resources>
      <script id="http://redhat.com/rhel/kickstart/jeos"/>
      <script id="http://redhat.com/rhel/kickstart/desktop"/>

(2)将找到的行重定向给/root/files文件:

[root@localhost ~]# grep re /usr/share/osinfo/os/redhat.com/rhel-9.1.xml > /root/files
[root@localhost ~]# cat /root/files
  <os id="http://redhat.com/rhel/9.1">
    <upgrades id="http://redhat.com/rhel/9.0"/>
    <derives-from id="http://redhat.com/rhel/9.0"/>
    <release-status>prerelease</release-status>
    <!-- RHEL 9.1 entries have to cope both with 9.1 and 9.1.x regexes for
         medias' volume-id and trees' version -->
    <tree arch="aarch64">
      <treeinfo>
      </treeinfo>
    </tree>
    <tree arch="ppc64le">
      <treeinfo>
      </treeinfo>
    </tree>
    <tree arch="s390x">
      <treeinfo>
      </treeinfo>
    </tree>
    <tree arch="x86_64">
      <treeinfo>
      </treeinfo>
    </tree>
    <resources arch="ppc64le">
      <recommended>
      </recommended>
    </resources>
    <resources arch="x86_64">
      <recommended>
      </recommended>
    </resources>
    <resources arch="s390x">
      <recommended>
      </recommended>
    </resources>
    <resources arch="aarch64">
      <recommended>
      </recommended>
    </resources>
    <resources arch="all">
      <recommended>
      </recommended>
    </resources>
      <script id="http://redhat.com/rhel/kickstart/jeos"/>
      <script id="http://redhat.com/rhel/kickstart/desktop"/>


4、将整个 /etc 目录下的文件全部打包并用 gzip 压缩成/back/etcback.tar.gz(使用tar指令)

(1)创建/back目录:

[root@localhost ~]# mkdir /back
[root@localhost ~]# ll -d /back
drwxr-xr-x. 2 root root 6  4月  9 22:32 /back

(2)使用tar命令进行打包和压缩:

[root@localhost ~]# tar -czf /back/etcback.tar.gz /etc/*
tar: 从成员名中删除开头的“/”
tar: 从硬连接目标中删除开头的“/”
[root@localhost ~]# ll /back
总用量 6192
-rw-r--r--. 1 root root 6336553  4月  9 22:35 etcback.tar.gz

(3)验证:

[root@localhost test]# tar -xf /back/etcback.tar.gz
[root@localhost test]# ll /root/test
总用量 12
drwxr-xr-x. 133 root root 8192  4月  9 22:41 etc

完成。
5、设置权限,要求如下:创建g1组,要求创建一个属于redhat用户g1组的文件redhat.txt(使用chown修改所属者和所属组)

(1)首先创建g1组,创建用户redhat:

[root@localhost ~]# groupadd g1
[root@localhost ~]# useradd redhat
[root@localhost ~]# grep redhat /etc/passwd
redhat:x:1001:1002::/home/redhat:/bin/bash
[root@localhost ~]# grep g1 /etc/group
g1:x:1001:

(2)创建文件redhat.txt:

[root@localhost ~]# touch /root/test/redhat.txt
[root@localhost ~]# ll -d /root/test/redhat.txt 
-rw-r--r--. 1 root root 0  4月 10 09:19 /root/test/redhat.txt

(3)使用chown修改所属组和所属者:

[root@localhost ~]# chown redhat:g1 /root/test/redhat.txt 
[root@localhost ~]# ll /root/test/redhat.txt 
-rw-r--r--. 1 redhat g1 0  4月 10 09:19 /root/test/redhat.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值