Extract user accounts and home directory from /etc/passwd.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract user accounts and home directory from /etc/passwd.
# 1  
Old 06-10-2014
Extract user accounts and home directory from /etc/passwd.

I am trying to obtain all user accounts and their respective home directories.

/etc/passwd contains the required information, but I want to filter it to only show the uid,username and home directory path.

I am working on a Solaris 11 machine.

I made a little headway so far, but I got stuck Smilie.
Code:
cat /etc/passwd | cut -f1,3,6 -d:

I have to eliminate results which have >100 uid, as they are not user accounts.

I tried using grep to do so, but it got really out of hand lol Smilie

Code:
cat /etc/passwd | cut -f1,3,6 -d: | grep '[0-9][0-9][0-9]:[a-z]+:/[a-z]+/[a-z]+/[a-z]+'

Any help is greatly appreciated.
# 2  
Old 06-10-2014
Code:
awk -F: ' $3 <= 100 {print $1, $6}' /etc/passwd

---------- Post updated at 11:16 PM ---------- Previous update was at 11:00 PM ----------

Code:
while IFS=: read user shadow uid guid geco home sh; do if [[ $uid -le 100 ]]; then echo $user $home; fi; done < /etc/passwd

This User Gave Thanks to Aia For This Post:
# 3  
Old 06-10-2014
Thanks a million, your 2nd script worked. I don't know what it is, but it is magical.

I actually wanted >100, so I changed le 100 to ge 100.

Cheers Smilie
# 4  
Old 06-10-2014
Quote:
Originally Posted by Hijanoqu
Thanks a million, your 2nd script worked. I don't know what it is, but it is magical.

I actually wanted >100, so I changed le 100 to ge 100.

Cheers Smilie
>100 is $uid -gt 100; $uid -ge 100 is >= 100.
# 5  
Old 06-10-2014
Quote:
Originally Posted by Hijanoqu
I tried using grep to do so, but it got really out of hand lol Smilie

Code:
cat /etc/passwd | cut -f1,3,6 -d: | grep '[0-9][0-9][0-9]:[a-z]+:/[a-z]+/[a-z]+/[a-z]+'

I think you got offered a better solution here, but in case your curiosity is still aroused, this is how grep/cut can do it:

grep's task is to select all lines where UID is below 100. I have no Solaris system at hand, so here is a passwd from an AIX system. The ideas how to filter are the same, though.

Code:
# cat /etc/passwd
root:!:0:0::/:/usr/bin/ksh
daemon:!:1:1::/etc:
bin:!:2:2::/bin:
sys:!:3:3::/usr/sys:
adm:!:4:4::/var/adm:
uucp:!:5:5::/usr/lib/uucp:
nobody:!:4294967294:4294967294::/:
lpd:!:9:4294967294::/:
lp:*:11:11::/var/spool/lp:/bin/false
invscout:*:6:12::/var/adm/invscout:/usr/bin/ksh
nuucp:*:7:5:uucp login user:/var/spool/uucppublic:/usr/sbin/uucp/uucico
snapp:*:200:13:snapp login user:/usr/sbin/snapp:/usr/sbin/snappd
ipsec:*:201:1::/etc/ipsec:/usr/bin/ksh
sshd:*:202:201::/var/empty:/usr/bin/ksh
opc_op:*:777:202:OVO default operator:/home/opc_op:/usr/bin/sh
apache:*:64500:64500:Apache User:/home/apache:/usr/bin/ksh
wikiroot:*:153:1:Wiki Admin:/var/www/htdocs:/usr/bin/ksh
mysql:*:251:251:MySQL system user:/opt/freeware/mysql:/usr/bin/ksh
ctmagp:*:38713:5003:CM Agent User:/home/ctmagp:/bin/ksh
tsmbkusr:*:38638:1:TSM-Backup User:/home/tsmbkusr:/bin/ksh
plotadm:*:252:1:tech. user fuer plotgenerierung:/home/plotadm:/usr/bin/ksh
nmon2rrd:*:253:1:user fuer generierung von nmon2rrd daten:/home/nmon2rrd:/usr/bin/ksh
collect:*:204:1:data collector:/home/collect:/usr/bin/ksh
lpar2rrd:*:205:1:tech user lpar2rrd:/var/datasource/lpar2rrd:/usr/bin/ksh

First, what is "lower than/greater than 100" in regexps? Below 100:
Code:
/[0-9]*[0-9]/

This covers every number with one or two digits 0-9, therefore "0-99". We can use "grep -v" to invert that regexp and get evrything greater/equal. Because this is the third field and is delimited by ":" we add these to the regexp to further reduce ambiguity:

Code:
/:[0-9]*[0-9]:/

Now, because we want this to only match the third field, not any other, what is the "third field" here? It is: some characters, followed by a colon, followed by some more characters, followed by a colon - and now any character up to the next colon. In regexp:

Code:
/^[^:]*:[^:]*:[0-9]*[0-9]:/

Now try it:

Code:
grep '^[^:]*:[^:]*:[0-9]*[0-9]:' /etc/passwd | cut -d':' -f1,3,6          # below UID 100
grep -v '^[^:]*:[^:]*:[0-9]*[0-9]:' /etc/passwd | cut -d':' -f1,3,6       # UID 100 and above

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 6  
Old 06-10-2014
I'm afraid that [0-9]*[0-9] will match any integer number, even longer than two digits. Try [0-9]?[0-9] instead. You may need to escape the quotation mark or use extended grep.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 06-10-2014
Quote:
Originally Posted by RudiC
I'm afraid that [0-9]*[0-9] will match any integer number, even longer than two digits.
Yes, you are right. My bad. Here is a corrected version of the sensitive part of the regexp:

Code:
/[0-9]\{1,2\}/

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

SunOS confusing root directory and user home directory

Hello, I've just started using a Solaris machine with SunOS 5.10. After the machine is turned on, I open a Console window and at the prompt, if I execute a pwd command, it tells me I'm at my home directory (someone configured "myuser" as default user after init). ... (2 Replies)
Discussion started by: egyassun
2 Replies

2. UNIX for Dummies Questions & Answers

User's home directory not being created

I am trying to create Oracle user. I will install oracle after that. But my problem is /home/oracle directory is not being created. bash-3.2# useradd -g oinstall -G dba,oper -d /home/oracle -m oracle cp: /home/oracle: Operation not applicable chown: /home/oracle: No such file or directory ... (3 Replies)
Discussion started by: hubatuwang
3 Replies

3. Red Hat

User's home directory

Hi, By default user's home directory will be /home/$user. I want to change it to /javauser/$user. How can I do it? Thanks Jeevan. (5 Replies)
Discussion started by: jredx
5 Replies

4. Solaris

Restricting SFTP user to a defined directory and home directory

Hi, I've created solaris user which has both FTP and SFTP Access. Using the "ftpaccess" configuration file options "guest-root" and "restricted-uid", i can restrict the user to a specific directory. But I'm unable to restrict the user when the user is logged in using SFTP. The aim is to... (1 Reply)
Discussion started by: sftpuser
1 Replies

5. Shell Programming and Scripting

change home directory by modifying passwd

hi How can I change the home directory of a user without using usermod -d command? ( by modifying /etc/passwd) (17 Replies)
Discussion started by: tjay83
17 Replies

6. Shell Programming and Scripting

how to find out the home directory of a user??

Hi all, I would like to know how to find out the home directory of a particular user.. eg, If am the root , then my Home directory will be / if say am just a user logging into the terminal then my home dir would change, so accordingly i would like to know how to find it out... I know that... (7 Replies)
Discussion started by: wrapster
7 Replies

7. Solaris

need script for locked and unused user accounts in /export/home directory

Hi all, i have to need one script: 1. it will capture the unused user accounts in /export/home directory. 2. it will capture the locked user accounts in /export/home directory. Note: locked accounts will show in /etc/passwd like /bin/false --> (instead of ksh it will show false) the... (1 Reply)
Discussion started by: krishna176
1 Replies

8. UNIX for Dummies Questions & Answers

user home directory problem

The home directory for me on my system is on /home/kwon. It was created using "useradd kwon" When i go to change the home directory for a user doing a usermod -d /home/test when they log on it gives them messages saying to generate new ssh keys, and it does. It gives me a thing that says... (1 Reply)
Discussion started by: BangYourWallnut
1 Replies

9. UNIX for Dummies Questions & Answers

resrtrict user to his home directory

Hello How do i restrict a user only to his own directory so that he wont be able to cd to other directories. say for excample there is user called xiamin then xiamin should be restricted to /usr/xiamin only. i am on redhat linux regards Hrishy (4 Replies)
Discussion started by: xiamin
4 Replies
Login or Register to Ask a Question