Showing posts with label distinct duplicate ps cut delimiter sort unique word count linux username. Show all posts
Showing posts with label distinct duplicate ps cut delimiter sort unique word count linux username. Show all posts

Thursday, August 12, 2010

Number of distinct users on linux/unix server

Today I was wondering if there was a way to figure out how many users were using a particular linux server. A simple thought that first came to my mind was couting the number of users in the output of 'ps -ef' command.

A simple 'wc -l' would give the number of lines in the output of 'ps -ef'.

The next step would be to just get the first column to get rid of the duplicate usernames.
'ps -ef | cut -d' ' f1' gave me the required output. Now the next step was to get only the unique entries. And the 'uniq' built in utility came to rescue.


The final command : 'ps -ef | cut -d' ' -f1 | sort | uniq | wc -l'

08/14/2010: I just realised that sort has an inbuilt option of unique
Instead of redirecting the out of sort to unique, one can use 'sort -u'

08/14/2010: Also instead of using 'ps -ef' command, one can use 'w'
Note: This command takes into account of the header row too. So the count would be count-- :)
08/14/2010: grep has an inbuilt option of '-v'which allows for invert matching. So the previous problem noted can be removed by adding 'grep -v UID'



The revised final command : 'w| cut -d' ' -f1 | sort -u | grep -v USER | wc -l'

08/14/2010: One final issue that I still see is the first line is taken into account here apart from the header.
 17:54:01 up 39 days, 10:20, 359 users,  load average: 0.59, 0.28, 0.15
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT