Wednesday, September 3, 2014

[Quick Tips] : How to list users above or below particular user id

How to list users above or below particular user id


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.
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
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.
See the below given output from above command

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
See the below given output

List only User name and its user id below user-id 500

See below given output:

No comments: