Get Linux User Account Info Using Bash Script

Thanks to

This article provides a bash script to get the information for a Linux user account. Script accepts the username as input and displays the following information about the user.

  • User Info
  • User Groups
  • User’s home directory and default shell
  • Total Size occupied by user’s home directory
  • User password and account expiry information

This script has been tested on RHEL5.3.

Script to Get Linux User Account Info

# cat
# Script Name:
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# Copyright
# Author: Kam Aggarwal, (
# Read the terms of use at

if [ "$1" = "" ]
        echo "Usage: $0 USERNAME"
        echo "Example:  $0 kam"

        exit 1

Username=`cat /etc/passwd | grep -Ew ^$1 | cut -d":" -f1`

if [ "$Username" = "" ]
        echo "Username $1 doesn't exist"
        exit 2

Userid=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f3`
UserPrimaryGroupId=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f4`
UserPrimaryGroup=`cat /etc/group | grep :"$UserPrimaryGroupId": | cut -d":" -f1`
UserInfo=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f5`
UserHomeDir=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f6`
UserShell=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f7`
UserGroups=`groups $Username | awk -F": " '{print $2}'`
PasswordExpiryDate=`chage -l $Username | grep "Password expires" | awk -F": " '{print $2}'`
LastPasswordChangeDate=`chage -l $Username | grep "Last password change" | awk -F": " '{print $2}'`
AccountExpiryDate=`chage -l $Username | grep "Account expires" | awk -F": " '{print $2}'`
HomeDirSize=`du -hs $UserHomeDir | awk '{print $1}'`

printf "%-25s : %5s  [User Id - %s]\n" "Username" "$Username" "$Userid"
printf "%-25s : %5s\n" "User Info" "$UserInfo"
printf "%-25s : %5s  [Group Id - %s]\n" "User's Primary Group" "$UserPrimaryGroup" "$UserPrimaryGroupId"
printf "%-25s : %5s\n" "User is Member of Groups" "$UserGroups"
printf "%-25s : %5s  [Size Occupied - %s]\n" "Home Directory" "$UserHomeDir" "$HomeDirSize"
printf "%-25s : %5s\n" "Default Shell" "$UserShell"
printf "%-25s : %5s\n" "Last Password Changed On" "$LastPasswordChangeDate"
printf "%-25s : %5s\n" "Password Expiry Date" "$PasswordExpiryDate"
printf "%-25s : %5s\n" "Account Expiry Date" "$AccountExpiryDate"

Script Usage

Download the script and transfer it to your Linux system. On my Linux system, this script resides in /root folder.

Provide executable permission to the script.

# chmod 755 /root/

You are ready to use the script. Following examples demonstrate the script usage.

Get Information of Linux User Kam

# sh kam

Username                  : kam  [User Id - 502]
User Info                 : Kam Aggarwal

User's Primary Group      : kam  [Group Id - 502]
User is Member of Groups  : kam root wheel bob

Home Directory            : /home/kam  [Size Occupied - 80K]
Default Shell             : /bin/bash

Last Password Changed On  : Sep 12, 2012
Password Expiry Date      : Oct 13, 2012
Account Expiry Date       : Dec 16, 2012

Get Information of Linux User root

# sh root

Username                  :  root  [User Id - 0]
User Info                 :  root

User's Primary Group      :  root  [Group Id - 0]
User is Member of Groups  : root bin daemon sys adm disk wheel

Home Directory            : /root  [Size Occupied - 1.3M]
Default Shell             : /bin/bash

Last Password Changed On  : Aug 30, 2012
Password Expiry Date      : never
Account Expiry Date       : never
Posted in Linux
One comment on “Get Linux User Account Info Using Bash Script
  1. Lynnette says:

    What’s up to all, the contents present at this site are actually awesome for people experience, well, keep up the good work fellows.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: