本文整理了一批 Linux 基礎文本處理與用戶管理的實操練習,涵蓋 who、cut、uniq、sort、tr、find 等常用命令的組合運用。
1、列出當前系統上所有已登錄用戶的用戶名(同一用戶多次登錄只顯示一次)。
[root@lanin ~]# who #顯示當前用戶登錄信息 root tty1 2016-08-15 15:58 root pts/0 2016-08-15 16:07 (192.168.1.118) [root@lanin ~]# who | cut -d' ' -f1 #取出用戶名 root root [root@lanin ~]# who | cut -d' ' -f1 | uniq #去重 root
2、取出最後登錄到當前系統的用戶相關信息。
[root@lanin ~]# who | tail -n 1 root pts/0 2016-08-15 16:07 (192.168.1.118) [root@lanin ~]# who | tail -n 1 | cut -d' ' -f1 root [root@lanin ~]# who | tail -n 1 | cut -d' ' -f1 | id uid=0(root) gid=0(root) 組=0(root)
3、取出當前系統上被用戶當作默認 shell 最多的那個 shell。
[root@lanin ~]# cut -d: -f7 /etc/passwd | uniq -c | sort -n #統計相同默認 shell 的用戶數量並排序 [root@lanin ~]# cut -d: -f7 /etc/passwd | uniq -c | sort -n | tail -n 1 #取最後一行即用戶最多的 shell
4、將 /etc/passwd 中第三個字段數值最大的後 10 個用戶信息全部轉為大寫後保存至 /tmp/maxusers.txt。
[root@lanin ~]# cat /etc/passwd | sort -n -k 3 -t: | tail -10 | tr 'a-z' 'A-Z' > /tmp/maxusers.txt
5、取出當前主機的 IP 地址(對 ifconfig 結果進行切分)。
ifconfig 輸出(節選):
eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@lanin ~]# ifconfig | grep 'inet addr' | head -1 | cut -d' ' -f2 | cut -d':' -f2 192.168.120.204
6、列出 /etc 目錄下所有以 .conf 結尾的文件名,轉為大寫後保存至 /tmp/etc.conf。
[root@lanin ~]# mkdir /tmp/etc [root@lanin ~]# find /etc/*.conf | tr 'a-z' 'A-Z' | cut -d/ -f3 >> /tmp/etc/conf [root@lanin ~]# more /tmp/etc/conf
7、顯示 /var 目錄下一級子目錄或文件的總個數。
[root@lanin /]# ls /var | wc -l
8、取出 /etc/group 文件中第三個字段數值最小的 10 個組的名字。
[root@lanin /]# cat /etc/group | sort -n -t: -k3 | head -n 10 | cut -d: -f1 root bin daemon sys adm tty disk lp mem kmem
9、將 /etc/fstab 和 /etc/issue 的內容合并後保存至 /tmp/etc.test。
[root@lanin /]# cat /etc/fstab /etc/issue >> /tmp/etc/test
10、用戶和組管理類命令的綜合練習:
(1) 創建組 distro,GID 為 2016 groupadd distro -g 2016 (2) 創建用戶 mandriva,ID 為 1005,基本組為 distro useradd mandriva -u 1005 -g distro (3) 創建用戶 mageia,ID 為 1100,家目錄為 /home/linux useradd mageia -u 1100 -d /home/linux (4) 給 mageia 添加密碼 mageedu echo 'mageedu' | passwd --stdin mageia (5) 刪除 mandriva,但保留其家目錄 userdel mandriva (6) 創建用戶 slackware,ID 為 2002,基本組 distro,附加組 peguin groupadd peguin useradd slackware -u 2002 -g distro -G peguin (7) 修改 slackware 的默認 shell 為 /bin/tcsh usermod -s /bin/tcsh slackware (8) 為 slackware 新增附加組 admins groupadd admins usermod -a -G admins slackware (9) 為 slackware 添加密碼,最短使用期限 3 天,最長 180 天,警告 3 天 echo "654321" | passwd --stdin slackware && chage -W 3 -E 180 -I 3 slackware (10) 添加用戶 openstack,ID 為 3003,基本組 clouds,附加組 peguin 和 nova useradd openstack -u 3003 -g clouds -G peguin,nova (11) 添加系統用戶 mysql,shell 為 /sbin/nologin useradd mysql -u 499 -s /sbin/nologin (12) 使用 echo 非交互式為 openstack 添加密碼 echo "openstack" | passwd --stdin openstack
本文出自 "11460225" 博客,請務必保留此出處 https://11470225.blog.51cto.com/11460225/1839853