retrieving usernames in a system in bash

540 Views Asked by At

I'm new to scripting in bash and would like to retrieve all user names of a Linux system along with their home directories. Each username got to be in a single line. Is this is the best way to do this? or is there any better way?

#! /bin/bash
# list all users accounts using the /etc/passwd file
cat /etc/passwd | awk -F: '{print $1," ",$6}' 

I ask you kindly to advise

1

There are 1 best solutions below

1
On

As @chepner commented, awk -F: '{print $1," ",$6}' /etc/passwd is better.

You can try the following :

 wget https://raw.githubusercontent.com/mbareck7/python_journey/main/nakerah/milestone00.py
 cp /etc/passwd milestone_00.txt
 python3 milestone00.py

It's a a python script which takes as input /etc/password file, and return a JSON list as bellow.

[                                                                                                                                                                                             
{                                                                                                                                                                                         
    "user_name": "root",                                                                                                                                                                  
    "user_id": "0",                                                                                                                                                                       
    "home_dir": "/root",                                                                                                                                                                  
    "login_shell": "/bin/bash",                                                                                                                                                           
    "normal_user": false                                                                                                                                                                  
},                                                                                                                                                                                        
{                                                                                                                                                                                         
    "user_name": "daemon",                                                                                                                                                                
    "user_id": "1",                                                                                                                                                                       
    "home_dir": "/usr/sbin",                                                                                                                                                              
    "login_shell": "/usr/sbin/nologin",                                                                                                                                                   
    "normal_user": false                                                                                                                                                                  
},......
 .........
]