In rare cases, you will find sometimes we need to list users above or below particular user id. Such requirement mostly come when you are playing around users related troubleshooting.
For an example, we want to list the users which are above userid 500 in Linux System.
The user related information like username and user id is available in /etc/passwd file in Linux/Unix like Operating System.
First check in which row the user id is present in /etc/passwd file. Generally it is located at 3 rd row in /etc/passwd file.
List users which has above user-id 500
List users which has above user-id 500, use below given syntax:
-F: means separator
$3 represents 3rd row
< 500 means above 500
awk -F: ‘$3 < 500' /etc/passwd
linux@tuxworld:~/Desktop$ awk -F: '$3 < 500' /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh www-data:x:33:33:www-data:/var/www:/bin/sh backup:x:34:34:backup:/var/backups:/bin/sh list:x:38:38:Mailing List Manager:/var/list:/bin/sh irc:x:39:39:ircd:/var/run/ircd:/bin/sh gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh libuuid:x:100:101::/var/lib/libuuid:/bin/sh syslog:x:101:103::/home/syslog:/bin/false colord:x:102:105:colord colour management daemon,,,:/var/lib/colord:/bin/false messagebus:x:103:107::/var/run/dbus:/bin/false lightdm:x:104:108:Light Display Manager:/var/lib/lightdm:/bin/false avahi-autoipd:x:105:112:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false avahi:x:106:113:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false usbmux:x:107:46:usbmux daemon,,,:/home/usbmux:/bin/false kernoops:x:108:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false pulse:x:109:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false rtkit:x:110:122:RealtimeKit,,,:/proc:/bin/false speech-dispatcher:x:111:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh hplip:x:112:7:HPLIP system user,,,:/var/run/hplip:/bin/false saned:x:113:123::/home/saned:/bin/false mysql:x:114:125:MySQL Server,,,:/nonexistent:/bin/false whoopsie:x:115:126::/nonexistent:/bin/false dhcpd:x:116:128::/var/run:/bin/false ftp:x:117:129:ftp daemon,,,:/srv/ftp:/bin/false bind:x:118:130::/var/cache/bind:/bin/false gdm:x:119:131:Gnome Display Manager:/var/lib/gdm:/bin/false jetty:x:120:132::/usr/share/jetty:/bin/false libvirt-qemu:x:121:133:Libvirt Qemu,,,:/var/lib/libvirt:/bin/false libvirt-dnsmasq:x:122:134:Libvirt Dnsmasq,,,:/var/lib/libvirt/dnsmasq:/bin/false debian-tor:x:123:135::/var/lib/tor:/bin/false apt-mirror:x:124:136::/var/spool/apt-mirror:/bin/sh vde2-net:x:125:137::/var/run/vde2:/bin/false sshd:x:126:65534::/var/run/sshd:/usr/sbin/nologin smmta:x:127:138:Mail Transfer Agent,,,:/var/lib/sendmail:/bin/false smmsp:x:128:139:Mail Submission Program,,,:/var/lib/sendmail:/bin/false postfix:x:129:140::/var/spool/postfix:/bin/false postgres:x:130:142:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash clamav:x:131:143::/var/lib/clamav:/bin/false festival:x:132:29::/home/festival:/bin/false ntp:x:133:144::/home/ntp:/bin/false linux@tuxworld:~/Desktop$
Now from above information, we will filter more. By using above output, we will list only User name and its user id.
List only User name and its user id above user-id 500
Use the below given Syntax
Here, {print $1,$3;} prints only row no. 1 and 3.
Whereas we have again used the -F: for separator.
awk -F: '$3 < 500' /etc/passwd|awk -F: '{print $1,$3;}'
See the below given output from above command
linux@tuxworld:~/Desktop$ awk -F: '$3 < 500' /etc/passwd|awk -F: '{print $1,$3;}' root 0 daemon 1 bin 2 sys 3 sync 4 games 5 man 6 lp 7 mail 8 news 9 uucp 10 proxy 13 www-data 33 backup 34 list 38 irc 39 gnats 41 libuuid 100 syslog 101 colord 102 messagebus 103 lightdm 104 avahi-autoipd 105 avahi 106 usbmux 107 kernoops 108 pulse 109 rtkit 110 speech-dispatcher 111 hplip 112 saned 113 mysql 114 whoopsie 115 dhcpd 116 ftp 117 bind 118 gdm 119 jetty 120 libvirt-qemu 121 libvirt-dnsmasq 122 debian-tor 123 apt-mirror 124 vde2-net 125 sshd 126 smmta 127 smmsp 128 postfix 129 postgres 130 clamav 131 festival 132 ntp 133 linux@tuxworld:~/Desktop$
Second Scenario : List users below particular user id
Use the given syntax for listing user below user id 500
-F: means separator
$3 represents 3rd row
> 500 means below 500
awk -F: '$3 > 500' /etc/passwd
See the below given output
linux@tuxworld:~/Desktop$ awk -F: '$3 > 500' /etc/passwd nobody:x:65534:65534:nobody:/nonexistent:/bin/sh sharad:x:1000:1000:sharad chhetri,111,91-000000,91-0000001:/home/sharad:/bin/bash linux:x:1002:1002::/home/linux:/bin/bash FOSS:x:1004:1004::/home/FOSS:/bin/sh linuxtolinux:x:1006:1006::/home/linuxtolinux:/bin/bash vbox:x:1007:1007::/home/vbox:/bin/sh ram:x:1008:1008::/home/ram:/bin/sh red:x:1009:1009::/home/red:/bin/sh joe:x:1010:1010::/home/joe:/bin/ksh john:x:1011:1011:John k,222,000001010,9100234-1,System administrator:/home/john:/bin/sh linux@tuxworld:~/Desktop$
List only User name and its user id below user-id 500
awk -F: '$3 > 500' /etc/passwd|awk -F: '{print $1,$3;}'
See below given output:
linux@tuxworld:~/Desktop$ awk -F: '$3 > 500' /etc/passwd|awk -F: '{print $1,$3;}' nobody 65534 sharad 1000 linux 1002 FOSS 1004 linuxtolinux 1006 vbox 1007 ram 1008 red 1009 joe 1010 john 1011 linux@tuxworld:~/Desktop$
Edward says
Why not simplify
awk -F: ‘$3 > 500’ /etc/passwd|awk -F: ‘{print $1,$3;}’
to
awk -F: ‘$3 > 500 {print $1,$3;}’ /etc/passwd
?
This saves at least one invocation of the ‘awk’ command ๐
Sharad Chhetri says
๐ It is there Edward.
Regards
Sharad