練習(xí)2

1.顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
1.1方法一:

[root@centos7 ~]# cat /proc/meminfo |head
MemTotal:        1863104 kB
MemFree:          139464 kB
MemAvailable:     843072 kB
Buffers:              40 kB
Cached:           818840 kB
SwapCached:           16 kB
Active:           971024 kB
Inactive:         419092 kB
Active(anon):     499964 kB
Inactive(anon):    93288 kB
[root@centos7 ~]# grep "^[sS]" /proc/meminfo
SwapCached:           16 kB
SwapTotal:       4194300 kB
SwapFree:        4193780 kB
Shmem:             22016 kB
Slab:             143520 kB
SReclaimable:      75672 kB
SUnreclaim:        67848 kB

1.2方法二:

[root@centos7 ~]# cat /proc/meminfo |head
MemTotal:        1863104 kB
MemFree:          139400 kB
MemAvailable:     843016 kB
Buffers:              40 kB
Cached:           818848 kB
SwapCached:           16 kB
Active:           971084 kB
Inactive:         419092 kB
Active(anon):     500016 kB
Inactive(anon):    93288 kB
[root@centos7 ~]# grep -i ^s /proc/meminfo
SwapCached:           16 kB
SwapTotal:       4194300 kB
SwapFree:        4193780 kB
Shmem:             22016 kB
Slab:             143520 kB
SReclaimable:      75672 kB
SUnreclaim:        67848 kB

2.顯示/etc/passwd文件中不以/bin/bash結(jié)尾的行

[root@centos7 ~]# grep -v "/bin/bash$" /etc/passwd |head 
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

3.顯示用戶rpc默認(rèn)的shell程序
3.1方法一:

[root@centos7 ~]# grep "^rpc\>" /etc/passwd
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
[root@centos7 ~]# grep "^rpc\>" /etc/passwd|cut -d: -f7|cut -d/ -f3
nologin

3.2方法二:

[root@centos7 ~]# grep "^rpc\>" /etc/passwd
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
[root@centos7 ~]# grep "^rpc\>" /etc/passwd | cut -d"/" -f6
nologin

3.3方法三:

[root@centos7 ~]# grep "^rpc\>" /etc/passwd
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
[root@centos7 ~]# grep "^rpc\>" /etc/passwd |grep  "[^/]\+$"
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
[root@centos7 ~]# grep "^rpc\>" /etc/passwd |grep -o "[^/]\+$"
nologin

3.4方法四:

[root@centos7 ~]# grep "^rpc\>" /etc/passwd
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
[root@centos7 ~]# grep "^rpc\>" /etc/passwd |grep  -o "[[:alpha:]]\+$"
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
[root@centos7 ~]# grep "^rpc\>" /etc/passwd |grep  -o "[[:alpha:]]\+$"
nologin

4.找出/etc/passwd中的兩位或三位數(shù)
4.1方法一:

[root@centos7 ~]# grep -o "\b[0-9]\{2,3\}\b" /etc/passwd |head 
12
11
12
100
14
50
99
99
192
192

4.2方法二:

[root@centos7 ~]# grep -o "\b[0-9]\{2,3\}\b" /etc/passwd |head 
12
11
12
100
14
50
99
99
192
192

5.顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面有非空白字符的行

[root@centos7 ~]# grep "^[[:space:]]\+[^[:space:]]\+" /etc/grub2.cfg |head
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry

6.找出“netstat -tan”命令結(jié)果中以LISTEN后跟任意多個空白字符結(jié)尾的行

[root@centos7 ~]# netstat -tan |grep "LISTEN[[:space:]]*$"
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 ::1:6010                :::*                    LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN 

7.顯示CentOS7上所有UID小于1000的用戶名和UID
7.1方法一:

[root@centos7 ~]# cut -d: -f1,3 /etc/passwd |grep '\<[0-9]\{1,3\}$' |tail
geoclue:990
pulse:171
gdm:42
saned:989
rpcuser:29
gnome-initial-setup:988
sshd:74
avahi:70
postfix:89
tcpdump:72

7.2方法二:

[root@centos7 ~]# cut -d: -f1,3 /etc/passwd |grep '\<[0-9]\{1,3\}\>' |tail
geoclue:990
pulse:171
gdm:42
saned:989
rpcuser:29
gnome-initial-setup:988
sshd:74
avahi:70
postfix:89
tcpdump:72

8.添加用戶bash、testbash、basher、sh、nologin(其中shell為/sbin/nologin),找出/etc/passwd用戶名和shell同名的行
8.1創(chuàng)建用戶

[root@centos7 ~]# useradd bash
[root@centos7 ~]# useradd testbash
[root@centos7 ~]# useradd basher
[root@centos7 ~]# useradd sh
[root@centos7 ~]# useradd -s /sbin/nologin nologin

8.2方法一:

[root@centos7 ~]# grep "^\([^:]\+\):.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1001:1001::/home/bash:/bin/bash
nologin:x:1005:1005::/home/nologin:/sbin/nologin

8.3方法二:

[root@centos7 ~]# grep "^\([a-z]\+\):.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1001:1001::/home/bash:/bin/bash
nologin:x:1005:1005::/home/nologin:/sbin/nologin

9.利用df和grep,取出磁盤各分區(qū)利用率,并從大到小排序
9.1方法一:

[root@centos7 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        895M     0  895M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M   11M  900M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda2       100G  4.7G   96G   5% /
/dev/sda3        50G   33M   50G   1% /data
/dev/sda1      1014M  179M  836M  18% /boot
tmpfs           182M  4.0K  182M   1% /run/user/42
tmpfs           182M   40K  182M   1% /run/user/0
/dev/sr0        4.4G  4.4G     0 100% /run/media/root/CentOS 7 x86_64
[root@centos7 ~]# df -h|grep "^/dev/sd"|tr -s " " %|cut -d% -f5|sort -nr
18
5
1

9.2方法二:

[root@centos7 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        895M     0  895M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M   11M  900M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda2       100G  4.7G   96G   5% /
/dev/sda3        50G   33M   50G   1% /data
/dev/sda1      1014M  179M  836M  18% /boot
tmpfs           182M  4.0K  182M   1% /run/user/42
tmpfs           182M   40K  182M   1% /run/user/0
/dev/sr0        4.4G  4.4G     0 100% /run/media/root/CentOS 7 x86_64
[root@centos7 ~]# df -h | grep "^/dev/sd" | grep -o "[0-9]\+%" | grep -o "[0-9]\+" | sort -nr 
18
5
1

9.3方法三:

[root@centos7 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        895M     0  895M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M   11M  900M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda2       100G  4.7G   96G   5% /
/dev/sda3        50G   33M   50G   1% /data
/dev/sda1      1014M  179M  836M  18% /boot
tmpfs           182M  4.0K  182M   1% /run/user/42
tmpfs           182M   40K  182M   1% /run/user/0
/dev/sr0        4.4G  4.4G     0 100% /run/media/root/CentOS 7 x86_64
[root@centos7 ~]# df -h | grep "^/dev/sd" | grep -o "[0-9]\+%"|tr -d %|sort -nr
18
5
1
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容