基础指令:sudo提权、通配符、特殊符号、stat命令、id命令、正则表达式

4.4 sudo提权

  • 作用:给普通用户做提权操作,在执行某个命令的时候,需要拥有的root的权限
  • 类似:在古代皇帝给某个人授权尚方大宝剑
授权的两种方法
第一种方式:
visudo [回车]

第二种方式:在101行进行写入
vim /etc/sudoers

案例100. 给ren用户授权cat命令

# Allow ordinary users to run any command anywhere,but you must use "sudo" command
ren     ALL=(ALL)       /usr/bin/cat

案例101. 给pengyu用户授权除了vim命令以外的命令

# Allow ordinary users to run any command anywhere,but you must use "sudo" command
pengyu     ALL=(ALL)       ALL,!/usr/bin/vim

4.4 通配符-查文件

符号 说明
* 表示任意所有
表示任意单个字符,扣掉
{} 文件名中有几个不同的选项,就可以使用此通配符 echo {1...10}.txt
[] 表示任意单个字符,[ab]表示a或者b;[ab] [ab]表示aa、ab、ba、bb;[0-9]表示匹配0到9数字之间命名的文件;[0-9a-z]匹配的是0-9数字之间和a-z字母之间命令的文件;

4.5 特殊符号

符号 说明
# 注释,管理员命令提示符
$ 普通用户的提示符,可以提取变量
; 命令分隔符,可以一次性执行多条命令,不管之前的命令是否执行成功
&& 命令分隔符,可以一次性执行多条命令,但是必须前一条命令执行成功才能执行后一条命令
`` 先执行里面的命令
$() 先执行里面的命令
' ' 所见即所得
" " 可以解析变量
! 取反、在命令行调用最后一次执行过的该次想要执行命令的命令
\ 转义,取消别名
$? 上一条命令的执行结果

案例102. 创建以主机名称和IP地址命令的一个目录

mkdir `hostname`_`hostname -I`

4.6 stat输出文件的详细内容

  • 可以以文本的格式显示文件或目录的详细信息
详细格式:stat [文件名/目录]
[root@kylin-ren-class ren]# stat vim.log 
  文件:“vim.log”
  大小:692646          块:1360       IO 块:4096   普通文件
设备:fd00h/64768d      Inode:34158030    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2025-01-02 21:11:51.047687991 +0800
最近更改:2024-12-25 10:16:24.000000000 +0800
最近改动:2025-01-02 21:08:12.509298384 +0800
创建时间:-

4.7 id命令查看用户基本信息

  • 查看用户的基本信息(包含用户的id,用户组id,附加组id)
语法结构:id [参数] [用户名]
参数 说明
-u 显示用户id
-g 显示主组id
-un 显示用户名
-gn 显示组名

4.8 正则表达式

4.8.1 符号 ^
符号 说明
[1] 找出以内容开头的行

案例103. 找出以o为开头的行

[root@kylin-ren-class ren]# grep '^o' 1.txt
our site is http:www.lizhenya.com

[root@kylin-ren-class ren]# cat 1.txt | grep '^o'
our site is http:www.lizhenya.com

案例104. 找出磁盘信息以tmp开头的行

[root@kylin-ren-class ren]# df -h | grep '^tmp'
tmpfs                  979M     0  979M    0% /dev/shm
tmpfs                  979M   17M  962M    2% /run
tmpfs                  979M     0  979M    0% /sys/fs/cgroup
tmpfs                  979M  4.0K  979M    1% /tmp
tmpfs                  196M     0  196M    0% /run/user/0

案例105. 找出以^开头的行

[root@kylin-ren-class ren]# grep '^^' 1.txt
4.8.2 符号 $
符号 说明
[内容]$ 以[内容]结尾的行

案例106. 找出以t结尾的行

[root@kylin-ren-class ren]# grep 't$' 1.txt 
test

案例107. 找出以a结尾的行

[root@kylin-ren-class ren]# grep 'a$' 1.txt 
lizhenyalizhenyalizhenya

案例108. 只显示/dev磁盘信息

[root@kylin-ren-class ren]# df -h | grep '/$'
/dev/mapper/klas-root   47G  3.9G   44G    9% /

案例109. cat -A可以在文本的最后加一个$符号,可以清晰的看到文本的每一行是以什么结尾的

[root@kylin-ren-class ren]# cat -A 1.txt 
I am lizhenya teacher!$
I teach linux.$
test$
$
I like badminton ball billiard ball and$
chinese chess!$
my blog is http:blog.51cto.com$
our site is http:www.lizhenya.com$
my qq num is 593528156$
$
aaaa,$
n0t572891888887.$
^^^^^^^66$$$$$$$^^^$$$
lizhenyalizhenyalizhenya$

案例110. 查找以 . 结尾的行,\表示转义

[root@kylin-ren-class ren]# grep '\.$' 1.txt
I teach linux.
n0t572891888887.

案例110. 查找以$结尾的行

[root@kylin-ren-class ren]# grep '$$' 1.txt
^^^^^^^66$$$$$$$^^^$$

案例111. 查找文本空行,^符号和$中间为空表示空行

[root@kylin-ren-class ren]# grep '^$' 1.txt


企业案例112. 找出文件中生效的行,排除空行和这是的行 -v 取反,一下命令的意思就是,找到除了#开头以及空行的行的内容

[root@kylin-ren-class ren]# grep -v '^#' /etc/selinux/config | grep -v '^$'
SELINUX=enforcing
SELINUXTYPE=targeted
SETLOCALDEFS=0
4.8.3 符号 .
符号 说明
. 表示任意单个字符,自动跳过空行

案例113. 显示匹配1.txt单个字符的过程,使用 -o 表示过程

[root@kylin-ren-class ren]# grep -o '[ab]' 1.txt
a
a
a
a
b
a
b
a
b
a
b
a
a
b
b
a
a
a
a
a
a
a
a

案例114. 统计文件中每个字母出现的次数,然后取top10

[root@kylin-ren-class ren]# cat 1.txt | grep -o '.' | sort | uniq -c | sort -nr | head
     22  
     17 a
     15 l
     15 i
     14 e
     12 t
     12 n
     11 h
     10 ^
      9 $

[root@kylin-ren-class ren]# grep -o '.' 1.txt | sort | uniq -c | sort -nr | head
     22  
     17 a
     15 l
     15 i
     14 e
     12 t
     12 n
     11 h
     10 ^
      9 $
# 解析:
1.首先cat命令用于传入一个文件的地址可以这样理解,如果不使用cat命令则需要在grep命令的后面加入文件地址
2.sort将每个字符都放在一起,因为uniq只能对挨着的进行统计,如果不挨着则算多个
3.uniq -c是使用去重统计,统计各个字符出现的次数
4.再次使用sort -nr进行对统计的结果进行排序
5.head取出结果的前十个内容

案例115. 统计单词出现的次数

[root@kylin-ren-class ren]# cat a.txt | xargs -n1 | sort | uniq -c | sort -rn
      3 shell
      2 mysql
      1 test
      1 renpengyu
      1 ren
      1 docker

[root@kylin-ren-class ren]# xargs -n 1 < a.txt | sort | uniq -c | sort -nr
      3 shell
      2 mysql
      1 test
      1 renpengyu
      1 ren
      1 docker

# 解析:
1.首先使用cat命令将数据文本内容传给xargs,这里注意不能省略cat命令,因为xargs不能处理文件,但是xargs可以处理读取到的数据。也可以使用<号来将处理的文本直接出入到xargs当中
2.然后使用xargs -n 1 ,这里表示将文本信息按照列排列,其中的“1”表示排1列
3.sort表示将内容进行排序,默认将相同的内容排在一起
4.uniq -c 表示去重统计,注意是挨着的内容进行统计,如果是不挨着的则认为是多个
5.sort -nr表示将内容进行按照数字的逆序进行排列
4.8.4 符号 *
符号 说明
* 前一个字符出现“0”次或“0次以上”

案例116. 查看“运维”出现的次数

[root@kylin-ren-class ren]# grep '运维*' a.txt
学的是运维 全名叫云计算运维

# 注意:-o表示显示过程
[root@kylin-ren-class ren]# grep -o '运维*' a.txt
运维
运维

案例117. 查看“t”出现的次数

[root@kylin-ren-class ren]# grep 't*' b.txt -o
t
t
t
t
t
t
t
t

案例118. 匹配文件中*的行

[root@kylin-ren-class ren]# grep '*' b.txt 
**My Day at the Zoo**

案例119. 匹配文件中的所有行

[root@kylin-ren-class ren]# grep '.*' b.txt

# 如果“.”和“*”同时使用,会将文中的空行排除掉
4.8.5 符号 []
符号 说明
[abc] 表示任意单个字符,a或b或c。支持序列[a-z],[A-Z],[0-9]

案例120. 匹配文档中a字符,b字符,c字符

[root@kylin-ren-class ren]# grep '[学运d]' a.txt 
#你好 任鹏宇 我在学习 
#学的是运维 全名叫云计算运维 
#运维
dfnaf
gsdge

案例121. 匹配文档中a-z和0-9的内容

[root@kylin-ren-class ren]# grep '[a-z0-9]' a.txt 
#66
#2352345
#435345
34241
4155
52356
5347
dfnaf
fagghre
bwrtgh
gsdge

案例122. 注意:在[ ]中的特殊字符都会还原本意

[root@kylin-ren-class ren]# grep '[!]$' a.txt 
#运维.!
#运维.!
#66.!
#66.!
#2352345.!
#2352345.!
#435345.!
#435345.!

案例123. 查找文中$符的行

grep '[$]' b.txt

[root@kylin-ren-class ren]# grep '[$]' b.txt
^^^^^^^^66$$$$$$$^^^$$

案例124. 查找“.”或是“!”的行

grep '[.!]' b.txt

[root@kylin-ren-class ren]# grep '[.!]' b.txt
The monkeys were swinging from branch to branch and making funny faces. I laughed a lot!
not 572891999997.

案例125. 查找“m"或”空格 “或”.“或”$“结尾的行

grep '[m .$]' b.txt

[root@kylin-ren-class ren]# grep '[m .!]' b.txt
Next, we went to the monkey area.
The monkeys were swinging from branch to branch and making funny faces. I laughed a lot!
--- 
not 572891999997.

案例126. 对o,u,r进行取反,在[^]是取反的意思

grep '[^our]' b.txt

[root@kylin-ren-class ren]# grep '[^our]' b.txt
**My Day at the Zoo**
Last Saturday, I went to the zoo with my family. 
We woke up early and packed a picnic lunch to enjoy there
When we arrived, I was so excited to see all the animals.

案例127. 对^abc进行取反操作

grep '[^^abc]' b.txt

**My Day at the Zoo**
Last Saturday, I went to the zoo with my family.
not 572891999997.
^^^^^^^^66$$$$$$$^^^$$

案例128. 找出以m或者o或者a开头的行

grep '^[moa]' b.txt

[root@kylin-ren-class ren]# grep '^[moa]' b.txt
aaaa,

案例129. 查找包含my的行 ,并且显示行号

grep -n '[m][y]' b.txt

[root@kylin-ren-class ren]# grep -n '[m][y]' b.txt
11:Last Saturday, I went to the zoo with my family.
4.8.6 扩展正则符号 +
符号 说明
+ 前一个字符出现1次及1次以上

[!CAUTION]

grep扩展正则使用grep -E 或者是 egrep

案例130. 找出9出现1次或1次以上的

[root@kylin-ren-class ren]# grep -E '9+' b.txt 
The nation's average temperature reached 10.92 C last year, 1.03 C higher than the historical average, making it the warmest year on record, according to the China Meteorological Administration.
not 572891999997.

案例131. 查看上一个案例的匹配流程

[root@kylin-ren-class ren]# grep -E -o '9+' b.txt 
9
9
99999

[!IMPORTANT]

精准匹配--> grep -w-->只会出现一个

案例132. 查看只有“ren”的行

[root@kylin-ren-class ren]# grep 'ren' c.txt 
renpengyu
renpengyu12323123
ren
renpengyu2234

[root@kylin-ren-class ren]# grep -w 'ren' c.txt 
ren
4.8.7 扩展正则符号 |
符号 说明
| 或者

案例133. 找出my或者not的行

[root@kylin-ren-class ren]# egrep 'my|not' b.txt 
Last Saturday, I went to the zoo with my family. 
not 572891999997.

案例134. 找出文件中的空行或者#的行

[root@kylin-ren-class ren]# egrep -v '^$|#' /etc/selinux/comfig

[root@kylin-ren-class ren]# egrep -v '^$|#' /etc/selinux/config 
SELINUX=enforcing
SELINUXTYPE=targeted
SETLOCALDEFS=0
4.8.8 扩展正则符号 {}
符号 说明
前1个字符至少出现n次
前1个字符出现最少n次,最多m次

案例135. 匹配最少出现2次的9

[root@kylin-ren-class ren]# egrep '9{2}' b.txt 
not 572891999997.

案例136. 匹配最少出现3次的9

[root@kylin-ren-class ren]# egrep '9{3}' b.txt 
not 572891999997.

案例137. 匹配至少2个8,最多3个8

[root@kylin-ren-class ren]# egrep '8{2,3}' d.txt 
888888
888
88
888

案例138. 取出正确的身份证号

15 位和 18 位身份证号的统一正则表达式:
^\d{15}$|^\d{17}(\d|X|x)$

完整的身份证号格式验证(包括出生日期):
^\d{6}(1[89]|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])\d{3}(\d|X|x)$
4.8.9 扩展正则符号 ()
符号 说明
() 表示一个整体

案例139. 匹配出“运维”出现1次及1次以上

[root@kylin-ren-class ren]# egrep '(运维)+' a.txt 
#学的是运维 全名叫云计算运维 .
#运维.!
#运维.!

案例140. 匹配year和and出现1次及1次以上的行

[root@kylin-ren-class ren]# egrep '运维|!' a.txt 
#学的是运维 全名叫云计算运维 .
#运维.!
#运维.!
#66.!
#66.!
#2352345.!
#2352345.!
#435345.!
#435345.!

案例141. 使用()作为一个整体

[root@kylin-ren-class ren]# egrep '任鹏(宇|彭)IT' a.txt 
任鹏宇IT
任鹏彭IT

案例142. 找出当前目录中所有文件中的“运维”的行

[root@kylin-ren-class ren]# grep -r '运维' /ren/*
/ren/a.txt:学的是运维 全名叫云计算运维
/ren/a.txt:运维
/ren/b.txt:运维
/ren/b.txt:运维
/ren/b.txt:运维

案例143. 将142案例中的结果,把“运维”替换成“运维工程师”

[root@kylin-ren-class ren]# grep -r -o '运维' /ren/*
/ren/a.txt:运维
/ren/a.txt:运维
/ren/a.txt:运维
/ren/b.txt:运维
/ren/b.txt:运维
/ren/b.txt:运维

grep -r -o '运维' /ren/* | sed 's/:.*//' | xargs sed -i 's#运维#运维工程师#g'

步骤;
1.首先使用grep将“运维”从/ren/*中筛选出来
2.然后使用sed 's/:.*//' 将冒号后面的所有内容替换为空,得到了含有“运维”的纯文本目录
3.然后将得到的目录使用xargs甩到后面传递给sed -i 's###g',sed -i参数可以永久修改文件内容,这样即可替换成功

  1. 内容 ↩︎

posted @ 2025-03-01 21:05  小时候老白啦  阅读(53)  评论(0)    收藏  举报