Linux中的cut、sort、uniq以及用户(组)管理类指令应用示例

Linux中的cut、sort、uniq指令

  • 列出当前系统上所有已经登录的用户的用户名,注意:同一用户登录多次,只显示一次。

解决思路:
先使用who指令显示出所有已登录的用户。
然后对who指令的输出进行切割得到想要的第一列内容,即只包含用户名。可以使用cut指令。
最后对cut的结果进行排序并去重。可以使用sort指令。  
借助管道符,可以方便的将以上三步放在一条指令中完成,示例如下:

Linux中的cut、sort、uniq以及用户(组)管理类指令应用示例
示例中cut指令在使用-d指定分隔符时,此处使用的是空格,所以表示为" "并且需要使用引号引起来。

cut指令常用方式:  
  cut OPTION... [FILE]...
    -d CHAR:以指定的字符为分隔符  
    -f FIELDS,#: 挑选出的字段  
        #:指定的单个字段  
        #-#:连续的多个字段  
        #,#:离散的多个字段  

  sort指令常用方式:  
    sort [OPTION]... [FILE]...
    -t CHAR:指定分隔符
    -k # :用于排序比较的字段
    -n:基于数值大小而非字符进行排序
    -r:逆序进行排序
    -f: 忽略字符大小写
    -u:重复的行只保留一份
    重复行:连续且相同
  • 列出最后登录到当前系统的用户的相关信息。

[lantian@bogon ~]$ last | head -1
lantian  pts/2192.168.249.1Sat Nov 12 15:06 - 15:06  (00:00)    

  • 找出当前系统上被用户当做其默认shell的最多的那个shell。

[lantian@bogon ~]$ cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -rn | head -1  
18 /sbin/nologin    

  • 将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。

[lantian@bogon ~]$ cat /etc/passwd | sort -t: -k3 -n | tail | tr [a-z] [A-Z] > /tmp/maxusers.txt  
[lantian@bogon ~]$ head -3 /tmp/maxusers.txt
CENTOS:X:1002:1002::/HOME/CENTOS:/BIN/BASH
BASH:X:1003:1003::/HOME/BASH:/BIN/BASH
TESTBASH:X:1004:1004::/HOME/TESTBASH:/BIN/BASH    

  • 列出当前主机的IP地址。  
    方法一(cut切割)

[root@localhost ~]# ifconfig | grep "\<inet\>" | cut -d" " -f10
192.168.249.128
192.168.249.129
127.0.0.1  

方法二(正则表达式)

[lantian@bogon ~]$ ifconfig | grep -o -E
"\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-2][0-3])\.[[:digit:]]+\.[[:digit:]]+\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5][0-4])"
| grep -v  "\.25"
 
192.168.249.128  
192.168.249.129  
127.0.0.1    

  • 列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。

[lantian@bogon ~]$ ls /etc/*.conf | cut -d/ -f3 | tr [a-z] [A-Z] > /tmp/etc.conf    
[lantian@bogon ~]$ head -5 /tmp/etc.conf
ASOUND.CONF
DNSMASQ.CONF
DRACUT.CONF
E2FSCK.CONF
HOST.CONF  

  • 显示/var目录下一级子目录或文件的总个数。

[lantian@bogon ~]$ ls -l /var | wc -l
25  

  • 取出/etc/group文件中第三个字段数值最小的10个组的名字。

[lantian@bogon ~]$ cat /etc/group | sort -t: -n -k3 | head | cut -d: -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem  

  • 将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。

[lantian@bogon ~]$ cat /etc/fstab /etc/issue > /tmp/etc.test  
[lantian@bogon ~]$ cat /tmp/etc.test  

#
# /etc/fstab
# Created by anaconda on Fri Oct 28 22:20:06 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=9bda2897-b3d8-4e6a-9c51-9b78c7a22414 /boot                   xfs       defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
\S
Kernel \r on an \m  

用户和组的管理类指令

Linux系统中用户和组管理类指令总结如下图所示:

Linux中的cut、sort、uniq以及用户(组)管理类指令应用示例
Linux中的cut、sort、uniq以及用户(组)管理类指令应用示例
Linux中的cut、sort、uniq以及用户(组)管理类指令应用示例  

  • 创建组distro,其GID为2016

[root@localhost ~]# groupadd -g 2016 distro    
[root@localhost ~]# grep 2016 /etc/group
distro:x:2016:  

  • 创建用户mandriva,其ID号为1005,基本组为distro

[root@localhost ~]# useradd -u 1005 -g distro mandriva  

  • 创建用户mageia,其ID号为1100,家目录为/home/linux

[root@localhost ~]# useradd -u 1100 -d /home/linux mageia  
[root@localhost ~]# grep 1100 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash  

  • 给用户mageia添加密码,密码为mageedu

[root@localhost ~]# passwd mageia
Changing password for user mageia.  
New password: mageedu
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: mageedu
passwd: all authentication tokens updated successfully.    

  • 删除mandriva,但保留其家目录

[root@localhost ~]# userdel mandriva    

  • 创建用户slackware,其ID号为2002,基本组为distro,附加组peguin

[root@localhost ~]#useradd -u 2002 -g distro -G peguin slackware  
[root@localhost ~]# cat /etc/passwd | grep 2002  
slackware:x:2002:2016::/home/slackware:/bin/bash  
[root@localhost ~]# id 2002  
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)    

  • 修改slackware的默认shell为/bin/tcsh

[root@localhost ~]# usermod -s /bin/tcsh slackware  
[root@localhost ~]# !cat  
cat /etc/passwd | grep 2002  
slackware:x:2002:2016::/home/slackware:/bin/tcsh    

  • 为用户slackware新增附加组admins

[root@localhost ~]# groupadd admins  
[root@localhost ~]# id slackware  
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)  
[root@localhost ~]# useradd -G peguin,admins slackware  
useradd: user 'slackware' already exists  
[root@localhost ~]# usermod -G peguin,admins slackware  
[root@localhost ~]# !id  
id slackware  
uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin),2018(admins)    

  • 为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天

[root@localhost ~]# passwd -n 3 -x 180 -w 3 slackware
Adjusting aging data for user slackware.
passwd: Success
[root@localhost ~]# chage -l slackware
Last password change : Nov 12, 2016
Password expires : May 11, 2017
Password inactive : never
Account expires : never
Minimum number of days between password change : 3
Maximum number of days between password change : 180
umber of days of warning before password expires : 3
[root@localhost ~]# passwd slackware
Changing password for user slackware.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.  

  • 添加用户openstack,其ID号为3003,基本组为clouds,附加组为peguin和nova

[root@localhost ~]# cat /etc/group |grep -E "clouds|peguin|nova"
peguin:x:2017:slackware  
[root@localhost ~]# groupadd nova
[root@localhost ~]# cat /etc/group |grep -E "clouds|peguin|nova"
peguin:x:2017:slackware
nova:x:2019:
[root@localhost ~]# useradd -u 3003 -g clouds -G peguin,nova openstack
useradd: group 'clouds' does not exist
[root@localhost ~]# groupadd clouds
[root@localhost ~]# !us
useradd -u 3003 -g clouds -G peguin,nova openstack
[root@localhost ~]# cat /etc/group |grep -E "clouds|peguin|nova"
peguin:x:2017:slackware,openstack
nova:x:2019:openstack
clouds:x:2020:
[root@localhost ~]# id opensack
id: opensack: no such user
[root@localhost ~]# id openstack
uid=3003(openstack) gid=2020(clouds) groups=2020(clouds),2017(peguin),2019(nova)  

  • 添加系统用户mysql,要求其shell为/sbin/nologin

[root@localhost ~]# id mysql
id: mysql: no such user
[root@localhost ~]# useradd -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=3004(mysql) gid=3004(mysql) groups=3004(mysql)
[root@localhost ~]# grep "mysql" /etc/passwd
mysql:x:3004:3004::/home/mysql:/sbin/nologin  

  • 使用echo命令,非交互式为openstack添加密码

[root@localhost ~]# echo 'Qwc45#r!g' | passwd --stdin openstack
Changing password for user openstack.
passwd: all authentication tokens updated successfully.    

原创文章,作者:N24_lantian,如若转载,请注明出处:http://www.178linux.com/59162

(0)
上一篇 2016-11-13 20:24
下一篇 2016-11-13 23:48

相关推荐

  • 文件管理及常用命令(一)

    cat命令: -n 增加行号 空行也增加行号          [root@localhost ~]# cat f1     abcd     abc…

    Linux干货 2016-08-08
  • 初窥门径shell脚本

    1.什么是shell脚本    首先它是一个脚本,并不能作为正式的编程语言。因为是跑在linux的shell中,所以叫shell脚本。确切的说shell脚本就是一些命令的集合。 2.写脚本前的一些细节及建议   Shell脚本通常都是以.sh 为后缀名的,这个并不是说不带.sh这个脚本就不能执行,只是大家的一个习惯而已…

    Linux干货 2016-08-15
  • 九.Linux博客-2016年8月10日脚本、sed、vim

    格式说明: 操作 概念 命令 说明及举例 九 脚本sed、vim sed 文本编辑工具 行编辑器,每次取出一行在内存里处理,处理完成后屏幕打印。完成后再取出一行放到内存里处理,覆盖原来内存中的行,循环。。 sed -n '3d' f1 把文件f1中的第三行删掉并不在屏幕上显示 sed -n…

    Linux干货 2016-08-24
  • N25期—第四周作业

    1、 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 cp –rf /etc/skel /home/tuser1 chmod -R go= /home/tuser1 2、 编辑/etc/group文件,添加组hado…

    Linux干货 2016-12-26
  • yum相关知识及源码编译安装http

    二.源码编译安装http 获得源码,将源码放在/usr/local/src下,并将其解压缩到此目录下 进入解压缩文件下,查看INSTALL文件 文件内容如下 黄色区域即为安装步骤 在当前目录下执行./configure –prefix=/usr/local/appach2 编译  make make install  把目标文…

    系统运维 2016-08-24
  • TCP连接的状态详解以及故障排查

    我们通过了解TCP各个状态,可以排除和定位网络或系统故障时大有帮助。(总结网络上的内容) 1、TCP状态 linux查看tcp的状态命令: 1)、netstat -nat  查看TCP各个状态的数量 2)、lsof  -i:port  可以检测到打开套接字的状况 3)、 &nbs…

    Linux干货 2015-04-03