Counting total users


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Counting total users
# 1  
Old 04-13-2010
Counting total users

wow, back again Smilie

I am trying to write a program that has many functions, however one of the functions will count the total number of users in the database.


I want to just use wc -l passwd (i've made a copy of passwd to play with, no worries lol)

but I am worried that if any of the accounts contained enough info to be longer than one line of text, it would mess up. Am I correct in my assumption, or does unix read an extended line still as one line?

should I use the 'cut' command to just read the first field? If so, how?

I dunno how different other versions of unix/linux are, but my passwd file is done like this:
root:x:0:0:root:root:/bin/bash


also, how would like to save the number of users as a variable, but not include the name of the file ('wc -l passwd' gives me a reply of # passwd, and i dont want the word passwd to show up)

thanks in advance!
# 2  
Old 04-13-2010
The passwd file only contains one user per line. If a users details appears to wrap to the next line, it's to do with your terminal size, not the file itself.

Use
Code:
VAR=$(wc -l < passwd)

to get rid of the filename.

HTH
# 3  
Old 04-13-2010
god bless u for answering both my questions Smilie
# 4  
Old 04-13-2010
You're welcome. But note that not all users in the passwd file are really "people". i.e. on my Mac there are 65 entries in the passwd file, and there's only me and root (also me) in the vicinity Smilie

Special "users" like daemon, nobody, lpr, etc. are used by the system for various functions.

If you have any convention by which user ID's are assigned, you may want to count only those that match.

i.e. assuming that "real" user IDs start from 1000:

Code:
awk -F: '$3 >= 1000 { C++ } END { print C+0 }' /etc/passwd

(or shorter, with wc)
Code:
awk -F: '$3 >= 1000' passwd | wc -l


Last edited by Scott; 04-13-2010 at 08:58 PM..
# 5  
Old 04-14-2010
Or you could count the users for whom a passwd has been set, e.g.
Code:
awk -F: 'length($2)>1' /etc/shadow | wc -l

depending on the security method used..
or
Code:
awk -F: 'length($2)>1{t++}END{print t+0}' /etc/shadow

Which presumably is a bit safer...

Last edited by Scrutinizer; 04-14-2010 at 03:30 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting total files with different file types in each folder

Trying to count total files with different file types with thousands of files in each folder. Since some files do not have extensions I have to use below criteria. Count Total Files starting with --> "^ERROR" Count Total Files starting with --> "^Runtime" Count Everything else or files... (3 Replies)
Discussion started by: kchinnam
3 Replies

2. Shell Programming and Scripting

To get total number of users and their groups in remote UNIX machine

Hi All I am trying to do ssh to different server and on the remote server for each user trying to get groups of that user but i am not getting the required result. ssh username@ip_address "for i in $( cat /etc/passwd| cut -d: -f1);do groups $i done;exit" >>abc.txt only names are... (5 Replies)
Discussion started by: Ekamjot
5 Replies

3. Shell Programming and Scripting

Create multiple users with individual passwords to users

hi, i am new to shell scripts i write a shell script to create multiple users but i need to give passwords to that users while creating users, command to write this script (1 Reply)
Discussion started by: DONFOX
1 Replies

4. Shell Programming and Scripting

Help with sum total number of record and total number of record problem asking

Input file SFSQW 5192.56 HNRNPK 611.486 QEQW 1202.15 ASDR 568.627 QWET 6382.11 SFSQW 4386.3 HNRNPK 100 SFSQW 500 Desired output file SFSQW 10078.86 3 QWET 6382.11 1 QEQW 1202.15 1 HNRNPK 711.49 2 ASDR 568.63 1 The way I tried: (2 Replies)
Discussion started by: patrick87
2 Replies

5. Shell Programming and Scripting

Total number of users logged in a server from uptime

how to find out total number of users logged in a server from uptime . i mean to say i need the total output of unix command . who gives the out put at a particular time . I need at all time from which machine who has connected , (3 Replies)
Discussion started by: amiya.te@gmail
3 Replies

6. Solaris

To restrict the users not to change the passwords for NIS users

Hi All, How to restrict the NIS users not to change their passwords in for NIS users?? and my NIS user is unable to login to at client location what could be the problem for this ? Any body can help me. Thanks in advance. (1 Reply)
Discussion started by: Sharath Kumar
1 Replies

7. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

8. Shell Programming and Scripting

counting users?

Is it possible to count the number of users? or specifically emac users? I know that you can count certain file sizes, like find /usr/bin/ -size 11k -exec ls {} \;|wc -1 but how would I count users? (3 Replies)
Discussion started by: gordonheimer
3 Replies

9. UNIX for Dummies Questions & Answers

grep running total/ final total across multiple files

Ok, another fun hiccup in my UNIX learning curve. I am trying to count the number of occurrences of an IP address across multiple files named example.hits. I can extract the number of occurrences from the files individually but when you use grep -c with multiple files you get the output similar to... (5 Replies)
Discussion started by: MrAd
5 Replies
Login or Register to Ask a Question