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.
-
Can you be a bit more specific? Do you look for the home directory of the user that runs grails, or for the home directory of an arbitrary user whose name or UID you have? – Torsten Marek Jan 29 '09 at 14:52
-
Arbitrary user on the system. – Jan 29 '09 at 16:01
-
Would the ~ directory constant help you in any way? – Richard C Jan 29 '09 at 14:51
13 Answers
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)

- 1
- 1

- 3,415
- 3
- 23
- 33
-
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. – Oct 09 '09 at 03:58
-
3The 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? – SpaceTrucker Feb 18 '13 at 08:21
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"}
).

- 302,674
- 57
- 556
- 614
-
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. – Oct 09 '09 at 04:01
-
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. – Joachim Sauer Oct 09 '09 at 06:59
-
1Running `echo ~` seems to do the trick if you're signed in as the user whose home directory you're looking for. – Danny Kopping Aug 18 '12 at 18:13
-
14To 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. – Santeri Paavolainen Oct 03 '12 at 14:19
-
1This 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. – doug65536 Nov 25 '13 at 06:13
-
@doug65536: so? That's why I wrote `echo ~username` and not just `echo ~`. – Joachim Sauer Nov 25 '13 at 08:28
-
@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. – doug65536 Nov 25 '13 at 11:45
-
In case you're not a Java developer, just like me. https://gist.github.com/zambon/9140388 – Henrique Zambon Feb 21 '14 at 18:37
-
As a sysadmin, switching to several user's home directories, this is helpful: `cd $(echo ~username)` – BhaveshDiwan Sep 25 '20 at 13:52
Try this on Java:
System.out.println("OS: " + System.getProperty("os.name") + ", USER DIRECTORY: " + System.getProperty("user.home"));

- 6,903
- 1
- 20
- 21
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();
}

- 130
- 2
- 7
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
.

- 66,273
- 12
- 162
- 149
-
-
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 Jan 21 '14 at 21:22
-
1<<< described as a "here string" at 19.1 of http://www.tldp.org/LDP/abs/html/abs-guide.html . – Bennett Brown Jan 21 '14 at 21:31
-
@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. http://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?" – Bruno Bronosky Jan 22 '14 at 02:22
You can use the environment variable $HOME
for that.

- 83,780
- 21
- 91
- 98
-
`$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. – Sebastian Simon Jan 05 '21 at 13:52
If you want to find a specific user's home directory, I don't believe you can do it directly.
When I've needed to do this before from Java I had to write some JNI native code that wrapped the UNIX getpwXXX()
family of calls.

- 334,560
- 70
- 407
- 495
I assume you want to find the home directory of a DIFFERENT user. Obviously getting the "user.home" property would be the easiest way to get the current user home directory.
To get an arbitrary user home directory, it takes a bit of finesse with the command line:
String[] command = {"/bin/sh", "-c", "echo ~root"}; //substitute desired username
Process outsideProcess = rt.exec(command);
outsideProcess.waitFor();
String tempResult;
StringBuilder sb = new StringBuilder();
while((tempResult = br.readLine()) != null) sb.append(tempResult);
br.close();
return sb.toString().trim();
Now technically, we should have a thread waiting on the stdout and stderr so the buffers don't fill up and lock up the process, but I'd sure hope the buffer could at least hold a single username. Also, you might want to check the result to see if it starts with ~root (or whatever username you used) just to make sure the user does exist and it evaluated correctly.
Hope that helps. Vote for this answer if it does as I'm new to contributing to SO and could use the points.

- 1,887
- 3
- 20
- 45
To find the home directory for user FOO on a UNIX-ish system, use ~FOO
. For the current user, use ~
.

- 17,381
- 6
- 41
- 47
Can you parse /etc/passwd?
e.g.:
cat /etc/passwd | awk -F: '{printf "User %s Home %s\n", $1, $6}'

- 600
- 3
- 9
-
1This doesn't work when anything but traditional unix user accounts is used (for example nis or winbindg). – Joachim Sauer Jan 29 '09 at 17:47
Find a Java wrapper for getpwuid/getpwnam(3)
functions, they ask the system for user information by uid or by login name and you get back all info including the default home directory.

- 14,535
- 3
- 29
- 30
The userdir prefix (e.g., '/home' or '/export/home') could be a configuration item. Then the app can append the arbitrary user name to that path.
Caveat: This doesn't intelligently interact with the OS, so you'd be out of luck if it were a Windows system with userdirs on different drives, or on Unix with a home dir layout like /home/f/foo, /home/b/bar.

- 21,744
- 6
- 51
- 55