Linux - 如何列出所有用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20268494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:29:25  来源:igfitidea点击:

Linux - How to list all users

linuxawkgrepuid

提问by Tadeusz Majkowski

How to write a script for linux wich list all users from /etc/passwd and their UID

如何为 linux 编写脚本,列出 /etc/passwd 中的所有用户及其 UID

User1 uid=0001
User2 uid=0002

...

...

the script shoul use: grep, cut, id, for

脚本应该使用:grep、cut、id、for

回答by Kent

awk -F: '
cut -d: -f1 /etc/passwd
= " uid="' /etc/passwd

awk is easier in this case.

在这种情况下,awk 更容易。

-F defines field separator as :

-F 将字段分隔符定义为 :

so you want is 1st and 3rd colums. so build the $0to provide your output format.

所以你想要的是第一和第三列。所以构建$0以提供您的输出格式。

this is very basic usage of powerful awk. you may want to read some tutorials if you faced this kind of problem often.

这是强大的 awk 的基本用法。如果您经常遇到此类问题,您可能需要阅读一些教程。

This time you got fish, if I were you, I am gonna do some research on how to fishing.

这次你钓到鱼了,如果我是你,我会研究钓鱼的方法。

回答by Will

cutis nice for this:

cut这很好:

awk  -F':' '>999 {print  " uid: " }' /etc/passwd | column -t | grep -v nobody

This means "cut, using :as the delimeter, everything except the first field from each line of the /etc/passwdfile".

这意味着“剪切,:用作分隔符,除/etc/passwd文件每一行的第一个字段外的所有内容”。

回答by Sanjar Stone

I think best option is as that:
grep "/bin/bash" /etc/passwd | cut -d':' -f1

我认为最好的选择是:
grep "/bin/bash" /etc/passwd | cut -d':' -f1

回答by sjas

##代码##

Gives you all regular users (uid >= 1000) neatly ordered including the userid.

为您提供所有常规用户 (uid >= 1000) 整齐排列,包括用户 ID。