How to check the home directory of a user in Linux via Command Line

Posted on

This is an actual article written in order to be able to achieve the main purpose described in the title of the article itself. The main purpose is for checking the home directory of a user in mainly Linux operating system. It can be done by executing a certain command via command line. The command itself is typed with the following pattern :

echo ~user

Using tilde is an unique sign for pointing out the /home/ folder which is specifically directed to the inner folder of /home/ named ‘user’. So, the folder full path is ‘/home/user’. The command ‘echo’ is used for printing the value of ‘~user’.

The above command is executed in the following pattern as shown below :

[root@hostname ~]# su - postgres
Last login: Wed Aug 1 10:53:35 2018 on pts/0
-bash-4.2$ echo ~postgres
/home/postgres
-bash-4.2$

The above command will print the output of the command as the value is ‘/home/postgres’. So, another thing which is needed to be considered is whether if user can print the home of another user.  It can be done as shown below :

[root@hostname ~]# echo ~postgres
/home/postgres
[root@hostname ~]# su - myuser
Last login: Wed Aug 1 16:43:06 2018 from hostname on pts/0
[myuser@hostname ~]$ echo ~postgres
/home/postgres
[myuser@hostname ~]$

As shown above, user root or other user named ‘myuser’ can actually print the variable pointing out the home folder of another user. Believe it or not, the command above is actually useful for printing the exact home folder of a user. Because sometime a user registered or created will not have a standard path of home folder. It can be located in other location if there is a requirement for the path itself is not located in ‘/home’. For example if the user created is specified with additional parameter -m to specify the location of it.  So, the command itself is very useful to accomplish it and it is useful to detect home folder which is not normally located in ‘/home/’.

Leave a Reply