-
列出当前系统上所有已经登陆的用户的用户名,注意一个用户登录多次,则只显示一次即可
[root@localhost ~]# who \\who命令显示已登录系统的用户
root pts/0 2017-08-14 21:49 (10.1.18.1)
centos1 pts/1 2017-08-14 21:49 (10.1.18.1)
centos2 pts/2 2017-08-14 21:49 (10.1.18.1)
[root@localhost ~]# who | cut -d " " -f1 \\who命令显示的结果通过管道交由cut处理,定义分隔符为空格,显示第一字段内容
root
centos1
centos2
[root@localhost ~]# who \\who命令显示centos2通过虚拟终端pts2和pst4登录系统
root pts/0 2017-08-14 21:49 (10.1.18.1)
centos1 pts/1 2017-08-14 21:49 (10.1.18.1)
centos2 pts/2 2017-08-14 21:49 (10.1.18.1)
centos3 pts/3 2017-08-14 21:54 (10.1.18.1)
centos2 pts/4 2017-08-14 21:54 (10.1.18.1)
[root@localhost ~]# who | cut -d " " -f1 \\取出用户名的字段
root
centos1
centos2
centos3
centos2
[root@localhost ~]# who | cut -d " " -f1 | sort -u \\通过sort进行排序去重
centos1
centos2
centos3
root
[root@localhost ~]# -
取出最后登录到当前系统的用户的相关信息
[root@localhost ~]# who
root pts/0 2017-08-14 21:49 (10.1.18.1)
centos1 pts/1 2017-08-14 21:49 (10.1.18.1)
centos4 pts/2 2017-08-14 22:11 (10.1.18.1)
centos3 pts/3 2017-08-14 21:54 (10.1.18.1)
centos2 pts/4 2017-08-14 21:54 (10.1.18.1)
[root@localhost ~]# who | sort -k 4
centos1 pts/1 2017-08-14 21:49 (10.1.18.1)
root pts/0 2017-08-14 21:49 (10.1.18.1)
centos2 pts/4 2017-08-14 21:54 (10.1.18.1)
centos3 pts/3 2017-08-14 21:54 (10.1.18.1)
centos4 pts/2 2017-08-14 22:11 (10.1.18.1)
[root@localhost ~]# who | sort -k 4 | tail -1
centos4 pts/2 2017-08-14 22:11 (10.1.18.1)
[root@localhost ~]# who | sort -r -k 4
centos4 pts/2 2017-08-14 22:11 (10.1.18.1)
centos3 pts/3 2017-08-14 21:54 (10.1.18.1)
centos2 pts/4 2017-08-14 21:54 (10.1.18.1)
root pts/0 2017-08-14 21:49 (10.1.18.1)
centos1 pts/1 2017-08-14 21:49 (10.1.18.1)
[root@localhost ~]# who | sort -r -k 4 | head -1
centos4 pts/2 2017-08-14 22:11 (10.1.18.1) -
取出当前系统上被用户当作其默认shell的最多的那个shell
[root@localhost ~]# cat /etc/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
...............
.............
[root@localhost ~]# cat /etc/passwd | cut -d ':' -f7
/bin/bash
/sbin/nologin
/sbin/nologin
.......
....
uniq -c
[root@localhost ~]# cat /etc/passwd | cut -d ':' -f 7 | sort
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/sync
.......
sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
........
先排序,在用uniq -u进行重复行统计(其只能进行连续重复行的统计) 数值大小排序 tail -1取出在最后一行
[root@localhost mytest]# cat /etc/passwd | cut -d ':' -f 7 | sort | uniq -c | sort -n | tail -1 | cut -c 9- \\显示第9个字符至行尾的内容
/sbin/nologin
-
将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存值/tmp/maxusers.txt文件中
[root@localhost mytest]# ll
总用量 0
[root@localhost mytest]# cat /etc/passwd | sort -t ":" -k 3 -n | tail -10 | tr [[:lower:]] [[:upper:]] > ./maxusers.txt
[root@localhost mytest]# ll
总用量 4
-rw-r--r--. 1 root root 520 8月 31 18:35 maxusers.txt
[root@localhost mytest]# cat maxusers.txt
GEOCLUE:X:996:994:USER FOR GEOCLUE:/VAR/LIB/GEOCLUE:/SBIN/NOLOGIN
CHRONY:X:997:995::/VAR/LIB/CHRONY:/SBIN/NOLOGIN
POLKITD:X:998:996:USER FOR POLKITD:/:/SBIN/NOLOGIN
SYSTEMD-BUS-PROXY:X:999:997:SYSTEMD BUS PROXY:/:/SBIN/NOLOGIN
CENTOS1:X:1000:1000::/HOME/CENTOS1:/BIN/BASH
CENTOS2:X:1001:1001::/HOME/CENTOS2:/BIN/BASH
CENTOS3:X:1002:1002::/HOME/CENTOS3:/BIN/BASH
CENTOS4:X:1003:1003::/HOME/CENTOS4:/BIN/BASH
FEDORA:X:1004:1005::/HOME/FEDORA:/BIN/BASH
NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
[root@localhost mytest]# cat /etc/passwd | sort -t ":" -k 3 -n | tail -10 | tr [[:lower:]] [[:upper:]] | tee ./maxusers_1.txt
GEOCLUE:X:996:994:USER FOR GEOCLUE:/VAR/LIB/GEOCLUE:/SBIN/NOLOGIN
CHRONY:X:997:995::/VAR/LIB/CHRONY:/SBIN/NOLOGIN
POLKITD:X:998:996:USER FOR POLKITD:/:/SBIN/NOLOGIN
SYSTEMD-BUS-PROXY:X:999:997:SYSTEMD BUS PROXY:/:/SBIN/NOLOGIN
CENTOS1:X:1000:1000::/HOME/CENTOS1:/BIN/BASH
CENTOS2:X:1001:1001::/HOME/CENTOS2:/BIN/BASH
CENTOS3:X:1002:1002::/HOME/CENTOS3:/BIN/BASH
CENTOS4:X:1003:1003::/HOME/CENTOS4:/BIN/BASH
FEDORA:X:1004:1005::/HOME/FEDORA:/BIN/BASH
NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
[root@localhost mytest]# ll
总用量 8
-rw-r--r--. 1 root root 520 8月 31 18:36 maxusers_1.txt
-rw-r--r--. 1 root root 520 8月 31 18:35 maxusers.txt - 取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分
[root@localhost mytest]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.1.18.120 netmask 255.255.255.0 broadcast 10.1.18.255
inet6 fe80::4148:d279:8d28:fcfd prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:27:57:5f txqueuelen 1000 (Ethernet)
RX packets 3191 bytes 260664 (254.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2545 bytes 2093954 (1.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:27:57:69 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 616 bytes 109296 (106.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 528 bytes 45880 (44.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 528 bytes 45880 (44.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:f5:12:2b txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost mytest]# ifconfig | grep "\<inet\>"
inet 10.1.18.120 netmask 255.255.255.0 broadcast 10.1.18.255
inet 127.0.0.1 netmask 255.0.0.0
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.25
[root@localhost mytest]# ifconfig | grep "\<inet\>" | cut -d " " -f 10
10.1.18.120
127.0.0.1
192.168.122.1
- 列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/etc/etc.conf文件中
[root@localhost mytest]# ls /etc/ | grep '.conf$' | tr [a-z] [A-Z] \\反斜杠为转义功能,保存就用重定向即可
ASOUND.CONF
BRLTTY.CONF
CHRONY.CONF
DLEYNA-SERVER-SERVICE.CONF
DNSMASQ.CONF
...................
思考:[root@localhost mytest]# ls /etc/*.conf 用globbing文件名通配,匹配出来,怎么将文件名转换为大写
- 显示/var目录下一级子目录或文件的总个数
[root@localhost mytest]# ls -A /var | cat -n
1 account
2 adm
3 cache
4 crash
5 db
6 empty
7 games
8 gopher
9 kerberos
10 lib
11 local
12 lock
13 log
14 mail
15 nis
16 opt
17 preserve
18 run
19 spool
20 tmp
21 .updated
22 yp
[root@localhost mytest]# ls -A /var | wc -w
22
- 取出/etc/group文件中第三个字段数值最小的10个组的名字
[root@localhost mytest]# cat /etc/group | sort -t ':' -nk 3 | head -10 | cut -d ':' -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
- **将/etc/fstab和/etc/issue文件的内容合并同一个内容后保存至/tmp/etc.test文件中
[root@localhost mytest]# cat /etc/fstab /etc/issue > /tmp/etc.test
网友评论