How to find a user's home directory on linux or unix?

200,913

Solution 1

For UNIX-Like systems you might want to execute "echo ~username" using the shell (so use Runtime.exec() to run {"/bin/sh", "-c", "echo ~username"}).

Solution 2

Normally you use the statement

String userHome = System.getProperty( "user.home" );

to get the home directory of the user on any platform. See the method documentation for getProperty to see what else you can get.

There may be access problems you might want to avoid by using this workaround (Using a security policy file)

Solution 3

Try this on Java:

System.out.println("OS: " + System.getProperty("os.name") + ", USER DIRECTORY: " + System.getProperty("user.home"));

Solution 4

For an arbitrary user, as the webserver:

private String getUserHome(String userName) throws IOException{
    return new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(new String[]{"sh", "-c", "echo ~" + userName}).getInputStream())).readLine();
}

Solution 5

If your are trying to do this for a user name that you cannot hard code, it can be challenging. Sure echo ~rbronosky would tell you the path to my home dir /home/rbronosky, but what if rbronosky is in a variable? If you do name=rbronosky; echo ~$name you get ~rbronosky

Here is a real world case and the solution:

You have a script that the user has to run via sudo. The script has to access the user's home folder. You can't reference ~/.ssh or else it will expand to /root/.ssh. Instead you do:

# Up near the top of your script add
export HOME=$(bash <<< "echo ~${SUDO_USER:-}")
# Then you can use $HOME like you would expect
cat rsa_key.pub >> $HOME/.ssh/authorized_keys

The beauty of it is that if the script is not run as sudo then $SUDO_USER is empty, so it's basically the same thing as doing "echo ~". It still works as you' expect.

If you use set -o nounset, which you should be using, the variable reference ${SUDO_USER:-} will default to blank, where $SUDO_USER or ${SUDO_USER} would give an error (because it is unset) if not run via sudo.

Share:
200,913
Dmitry Pashkevich
Author by

Dmitry Pashkevich

I have a passion for creating Web applications with a great user experience in mind. I love seeing how the Web enables people to manipulate information and collaborate with each other. I am detail-oriented and love working on tricky problems, sharing my experience and teaching others. I constantly strive to learn new topics and improve my own productivity.

Updated on November 07, 2020

Comments

  • Dmitry Pashkevich
    Dmitry Pashkevich over 3 years

    How do I find the home directory of an arbitrary user from within Grails? On Linux it's often /home/user. However, on some OS's, like OpenSolaris for example, the path is /export/home/user.

    • Richard C
      Richard C over 15 years
      Would the ~ directory constant help you in any way?
    • Admin
      Admin over 15 years
      Arbitrary user on the system.
  • Joachim Sauer
    Joachim Sauer over 15 years
    This doesn't work when anything but traditional unix user accounts is used (for example nis or winbindg).
  • Joachim Sauer
    Joachim Sauer over 15 years
    That's interpreted by the shell only, not the kernel/libraries.
  • Admin
    Admin over 14 years
    The app server will run as a production user and be located in /opt, for example. This may, or may not work, depending on how the production account is set up. Therefore, asking for the production user's home directory might not be the answer.
  • Admin
    Admin over 14 years
    Shelling out an doing an 'echo ~username' seems like the neatest answer. I see so many people who type 'cd /home/username' instead of 'cd ~username' ... Of course, you should never shell out, if you don't have to.
  • Joachim Sauer
    Joachim Sauer over 14 years
    But since Java doesn't provide an API for this, the only alternative I can think of is JNI, which is even worse than calling a shell.
  • Danny Kopping
    Danny Kopping over 11 years
    Running echo ~ seems to do the trick if you're signed in as the user whose home directory you're looking for.
  • Santeri Paavolainen
    Santeri Paavolainen over 11 years
    To be super-precise here -- /bin/sh is guaranteed under POSIX to be a Bourne-compliant shell, but tilde expansion (~ and ~user) is not one of the required features of such shell. Linux distributions use BASH as /bin/sh, for which tilde expansion works. If you run echo ~ using /bin/sh under some other UNIX OS, you can get back just the tilde (e.g. "~") instead of home directory path.
  • SpaceTrucker
    SpaceTrucker about 11 years
    The OP was asking for the home of an arbitrary user. But user.home is the one of the current user. Is this method always working if you would go from the current users home to the parent and then down to the directory with the specified users name?
  • riderchap
    riderchap almost 11 years
    Thanks. What was I looking for. This is useful in shell script.
  • doug65536
    doug65536 over 10 years
    This does not answer the question. They didn't ask how to find their own home directory, they asked how to find a user's home directory.
  • Joachim Sauer
    Joachim Sauer over 10 years
    @doug65536: so? That's why I wrote echo ~username and not just echo ~.
  • doug65536
    doug65536 over 10 years
    @JoachimSauer Ah, ok, my mistake. I didn't realize that "username" was a placeholder for the username whose directory path you want to get. I just tried it on my system and it did work. Thanks.
  • Bennett Brown
    Bennett Brown over 10 years
    What is the meaning of the triple <<< ? Googling redirection, here, ortriple lt fails to inform me, and I find nothing in print references.
  • Bennett Brown
    Bennett Brown over 10 years
    <<< described as a "here string" at 19.1 of tldp.org/LDP/abs/html/abs-guide.html .
  • Bruno Bronosky
    Bruno Bronosky over 10 years
    @BBrown, I'm glad you found it. Here strings are a way to avoid unnecessary subshells. I'm surprised they are not mentioned in the Useless Use of echo Awards. partmaps.org/era/unix/award.html#echo Now that you know about herestrings (like heredocs) imagine how unreadable it would have been without it... export HOME=$(echo "echo ~$SUDO_USER" | bash) "echo echo? what?"
  • Henrique Zambon
    Henrique Zambon about 10 years
    In case you're not a Java developer, just like me. gist.github.com/zambon/9140388
  • BhaveshDiwan
    BhaveshDiwan over 3 years
    As a sysadmin, switching to several user's home directories, this is helpful: cd $(echo ~username)
  • Sebastian Simon
    Sebastian Simon over 3 years
    $HOME changes depending on which user is logged in. I don’t see how it can be used to find the home of an arbitrary user.