- Get home directory by username
- 7 Answers 7
- There is a safe way to do this!
- on Linux/BSD/macOS/OSX without sudo or root
- Compare to how other answers respond to code injection
- on Linux/BSD/macOS/OSX as root
- on Linux/BSD (but not modern macOS/OSX) without sudo or root
- How to Find User’s Home Directory in Linux or Unix
- Ways to Find User’s Home Directory in Linux
- Navigating to the Home Directory Traditionally
- Find User’s Home Directory Using Cd Command
- How to get HOME, given USER?
- 6 Answers 6
Get home directory by username
Please do not use eval or bash -c with a variable. I added an answer that works safely for an Linux/Unix/macOS system with bash (even if you are not using bash as your shell, it likely has bash available because bashisms are everywhere). superuser.com/a/1613980/3376
7 Answers 7
But see Andrew’s comment and glenn’s reply below.
In bash eval isn’t needed it with just echo ~$username it’s okay, but in sh eval is needed if is a variable
This sometimes gives the wrong value, maybe the home folder of a previous account with the same username?
@choroba Add a user, delete the user, then add a user with the same username. If the user’s home folder is different the second time, this command gives the original home folder rather than the current one. glenn jackman’s answer gives the current one.
Can confirm that this won’t work at all if you have anything other than letters and numbers in a username. The method provided by Evan Carroll & Glen Jackmans answer below appears to work at least on Ubuntu 18. E.g: $( getent passwd «john-smith» | cut -d: -f6 )
homedir=$( getent passwd "$USER" | cut -d: -f6 )
This will also work on users that are not you. For instance,
homedir=$( getent passwd "someotheruser" | cut -d: -f6 )
This is legit, using getenv rather than assuming the location of passwd is even a step further than assuming the location of home is /home/
It seems you are that user — why not
This won’t work if you are in a sudo’ed environment and did not pass sudo the -H or -i flags — $HOME will still be the previous (sudo’ing) user’s home directory.
There is a safe way to do this!
on Linux/BSD/macOS/OSX without sudo or root
user=pi user_home=$(bash -c "cd ~$(printf %q "$user") && pwd")
NOTE: The reason this is safe is because bash (even versions prior to 4.4) has its own printf function that includes:
%q quote the argument in a way that can be reused as shell input
Compare to how other answers respond to code injection
# "ls /" is not dangerous so you can try this on your machine # But, it could just as easily be "sudo rm -rf /*" $ user="root; ls /" $ printf "%q" "$user" root\;\ ls\ / # This is what you get when you are PROTECTED from code injection $ user_home=$(bash -c "cd ~$(printf "%q" "$user") && pwd"); echo $user_home bash: line 0: cd: ~root; ls /: No such file or directory # This is what you get when you ARE NOT PROTECTED from code injection $ user_home=$(bash -c "cd ~$user && pwd"); echo $user_home bin boot dev etc home lib lib64 media mnt ono opt proc root run sbin srv sys tmp usr var /root $ user_home=$(eval "echo ~$user"); echo $user_home /root bin boot dev etc home lib lib64 media mnt ono opt proc root run sbin srv sys tmp usr var
on Linux/BSD/macOS/OSX as root
If you are doing this because you are running something as root then you can use the power of sudo:
user=pi user_home=$(sudo -u "$user" sh -c 'echo $HOME')
on Linux/BSD (but not modern macOS/OSX) without sudo or root
If not, the you can get it from /etc/passwd . There are already lots of examples of using eval and getent , so I’ll give another option:
user=pi user_home=$(awk -v u="$user" -v FS=':' '$1==u ' /etc/passwd)
I would really only use that one if I had a bash script with lots of other awk oneliners and no uses of cut . While many people like to «code golf» to use the fewest characters to accomplish a task, I favor «tool golf» because using fewer tools gives your script a smaller «compatibility footprint». Also, it’s less man pages for your coworker or future-self to have to read to make sense of it.
How to Find User’s Home Directory in Linux or Unix
Under a Linux operating system distribution environment, a created/existing system user is associated with a Home directory. The configuration of the Home directory ensures that the files belonging to the currently active Linux user are only accessible to that user unless this user switches to another user account where they will access the Home directory of that switched user.
The files under a Linux Home user directory are specific to the currently active users. The base directory of the Linux operating system is the root (/) directory.
It is from the root (/) directory that we should be able to access the Home (/home) directory.
If you only have a single active user on your Linux operating system environment, dealing with the Home directory is straightforward. The latter statement implies that every created/existing Linux user will have their system username as a directory name under this Linux Home directory.
For instance, listing the directories in the above Home directory lists three other directories to imply that the Linux operating system in question hosts 3 three users.
If we decide to navigate into either of the above Linux user folders, we should first be able to meet the following prerequisite.
It is only by being a sudoer/root user that we can be able to navigate into other Linux users’ Home directories without bumping into permission/access barriers.
From the above display, we have managed to navigate to the Home directory and list the files, folders, and directories associated with user dnyce whose user directory exists within the Home (/home) directory.
The above screen capture also reveals to us the different file permissions associated with the listed files, folders, and directories. The file permissions starting with – e.g – rw-rw-r— , imply that we are dealing with a file and the file permissions starting with d e.g drwxr-xr-x , imply that we are dealing with a folder or directory.
Ways to Find User’s Home Directory in Linux
Before we look at some viable approaches to finding a user’s home directory in Linux, it is important to understand why the Home directory exists. This directory helps differentiate system-wide data from user data such that we do not have to deal with redundancy. Also, important file backup operation becomes flawless.
Navigating to the Home Directory Traditionally
You first need to be sure that the Linux user exists. This approach is a summary of the above-discussed content.
The tilde (~) symbol indicates that we are at the home directory of the currently active user.
The Linux user’s home directory contains directories like Documents, Downloads, Music, Pictures, and Public.
Find User’s Home Directory Using Cd Command
Executing the cd (change directory) command alone should take you to the home directory of the current Linux user.
Another approach is to use cd + tilde (~) should navigate us to the Home directory of the currently logged-in user.
You can also use $HOME command, which takes you to the Home directory as a variable.
Not only do we understand the concept of the Linux user’s home directory, but we can navigate to it from any directory path.
How to get HOME, given USER?
I have an USER variable in my script, and I want to see his HOME path based on the USER variable. How can I do that?
6 Answers 6
There is a utility which will lookup user information regardless of whether that information is stored in local files such as /etc/passwd or in LDAP or some other method. It’s called getent .
In order to get user information out of it, you run getent passwd $USER . You’ll get a line back that looks like:
[jenny@sameen ~]$ getent passwd jenny jenny:*:1001:1001:Jenny Dybedahl:/home/jenny:/usr/local/bin/bash
Now you can simply cut out the home dir from it, e.g. by using cut, like so:
[jenny@sameen ~]$ getent passwd jenny | cut -d: -f6 /home/jenny
getent is the better answer, specially with remote user. in simpler systems ~user should be enough. Modded you up.
@muru — that’s true — and is spec’d behavior. the ~ does seem to tab-expand, though, and another spec’d behavior of the tilde-prefix shall be replaced by a pathname of the initial working directory associated with the login name obtained using the getpwnam() function and so probably that lookup is pretty good. i dont like tab-expansions, though — i like to type tabs.
Note that this only gets you the initial value of $HOME; the user’s login scripts are perfectly entitled to change it to something else. This is an unusual thing to do, but I can think of situations where it would be sensible, e.g. choosing between a local and an NFS-mounted homedir.
You can use eval to get someone’s home directory.
At least for local users this works for sure. I don’t know if remote users like LDAP are handled with eval .
Interesting, thanks, however this only gets the directory of the current user, not of other users. I think LDAP users are not handled, though I can be wrong.
@RuiFRibeiro It will work fine with LDAP, just use whatever variable contains your LDAP user’s username instead of USER .
Don’t use this without verifying that $USER expands to just a single string of alphabetic characters.
The usual place is /home/$USER , but that does not have to be universal. The definitive place to search for such information is inside the file /etc/passwd .
That file is world readable (anyone could read it), so any user has access to its contents.
If the $USER exists in the file, the entry previous to last is the user HOME directory.
This will select the entry and print the HOME directory:
awk -v FS=':' -v user="$USER" '($1==user) ' "/etc/passwd"
For more complex (remote) systems, getent is the usual command to get users information from the NSS (Name Service Switch libraries) system.
getent passwd "$USER" | cut -d : -f 6
Will provide equivalent information (if available).