How to Get the Home Directory of an User

To perform disk usage monitoring of users on a UNIX system, you will, first of all, need to determine the home directories of the users. You might ask, “Aren’t all the user home directories found under /home?”. Well, typically yes, but it’s not always true.

There are a couple of ways to determine the home directory of an user. I know of two methods at the moment.

  1. Extract the home directory field from /etc/passwd
    root@anfield:~# grep ibrahim /etc/passwd | awk -F":" '{ print $6 }'
    /users/ibrahim
    
  2. Use Tilde Expansion – this is useful when the user management/authentication is done via NIS or LDAP.
    root@anfield:~# echo ~ibrahim
    /users/ibrahim
    

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }