- How to find a user’s home directory on linux or unix?
- 13 Answers 13
- Get home directory in Linux
- 3 Answers 3
- What is the home directory on Windows Subsystem for Linux?
- 11 Answers 11
- Linux Home Directory
- Introduction of Linux Home Directory
- How Linux Home Directory Command Works?
- Examples to Implement Linux Home Directory
- 1. Home Directory
- 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 a user’s home directory on linux or unix?
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.
directory constant help you in any way?
13 Answers 13
Normally you use the statement
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)
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’ 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.
seems to do the trick if you’re signed in as the user whose home directory you’re looking for.
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.
Try this on Java:
For an arbitrary user, as the webserver:
You can use the environment variable $HOME for that.
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.
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:
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.
Источник
Get home directory in Linux
I need a way to get user home directory in C++ program running on Linux. If the same code works on Unix, it would be nice. I don’t want to use HOME environment value.
AFAIK, root home directory is /root. Is it OK to create some files/folders in this directory, in the case my program is running by root user?
is it not? As in cd
is implemented by the shell, not the kernel or libc. When programming in C++, you need to implement that yourself.
3 Answers 3
You need getuid to get the user id of the current user and then getpwuid to get the password entry (which includes the home directory) of that user:
Note: if you need this in a threaded application, you’ll want to use getpwuid_r instead.
You should first check the $HOME environment variable, and if that does not exist, use getpwuid.
Also note, that if you want the home directory to store configuration or cache data as part of a program you write and want to distribute to users, you should consider following the XDG Base Directory Specification. For example if you want to create a configuration directory for your application, you should first check $XDG_CONFIG_HOME using getenv as shown above and only fall back to the code above if the variable is not set.
If you require multi-thread safety, you should use getpwuid_r instead of getpwuid like this (from the getpwnam(3) man page):
Источник
What is the home directory on Windows Subsystem for Linux?
When I start bash on Windows Subsystem for Linux, it drops me in the directory
When I ls this directory, I see the directories Desktop , Documents , etc. that I can see are in C:\Users\ using the Windows File Explorer. Since this is where the bash program started, I expected this is my home directory, but when I type either cd or cd
I am brought to
which contains my .bashrc , .profile etc. file which I would expect to find in my home directory in a Linux box. Moreover, entering cd ../.. from here I can see the directories bin , etc and so on, again as expected on a Linux box.
I have two questions regarding all this:
- What exactly is /mnt/c/Users/ compared with C:\Users\ ? It seems they are one in the same—so what is /mnt/c/ ?
- How can I view the files in /home/ using the Windows File Explorer? Not that I’d really ever want to—I’m just trying to get a feel for how Windows is organizing this Subsystem for Linux thing.
11 Answers 11
In the latest versions [2020], the file system is accessed from:
Previously, as of 2018, The current path is related to which distribution you have installed from the Microsoft Store rather than one global path; for Ubuntu, it was located at:
Other distributions should be (to be confirmed) in a similar location under their respective folders within:
In Bash, to view the current directory in Windows File Explorer just enter:
Don’t leave out the «.».
This will open windows explorer at the current folder and you can see where everything is in relation to the rest of your Windows system.
- /mnt/c is the exact same as C:\ . It’s just the syntax for getting to it from WSL.
- Look in C:\Users\ \AppData\Local\Lxss\rootfs .
About the filesystem mounted on /mnt/c
- What exactly is /mnt/c/Users/ compared with C:\Users ? It seems they are one in the same—so what is /mnt/c/?
In contrast to Windows, Linux (and the other systems based on Unix) use a single folder structure independent of the number of disks you have. If you have multiple disks, all these disks must me mounted into the folder structure at some point.
- Typically, all the disks (different than the used to boot the system) are mounted in a folder named /mnt or /media
WSL has an special type of filesystem named DrvFS that gives you access to the disks used in windows. You can use DrvFS to mount, not only your windows filesystem, but also network disks and other media types.
- In WSL, by default, the C: disk in windows is mounted under /mnt/c
- If you have another disk, for instance a D: disk in windows, it will be mounted under /mnt/d
The files you can see in /mnt/c are the same you have in C: . If you modify some file, you will get the changes in the windows too.
About the location of /home/
- How can I view the files in /home/ using the Windows File Explorer? Not that I’d really ever want to—I’m just trying to get a feel for how Windows is organizing this Subsystem for Linux thing.
In WSL, all the linux filesystem is located under a Windows folder. The location of the folder depends on the version of Windows and of the WSL distribution you are using.
- Initial versions of WSL store the linux filesystem in %LOCALAPPDATA%\Lxss\rootfs
- WSL distributions installed from the Windows Store, starting in Windows Build 16215 (mid of 2017), use a folder like %LOCALAPPDATA%\Packages\
\LocalState\rootfs . The name of the package varies depending on the distribution (e.g. it is different for Ubuntu than for Debian). For Ubuntu on Windows it is CanonicalGroupLimited.UbuntuonWindows_ , for example.
- Linux distributions installed using other tools, such as lxRunOffline or WSL-DistroLauncher may store the linux filesystem into any location.
You may check many options to know the location of the WSL folder. For instance, I think the easiest option is to use lxRunOffline to know the installation folder.
Once you know the location of the installation folder, the /home/ is under \rootfs\home\username .
- For instance, if your installation folder is c:\wsl\ubuntu
- the /home/ is in c:\wsl\ubuntu\rootfs\home\username
NOTE: Both Linux and Windows stores file permissions in different ways. Nowadays, the WSL DrvFS stores the Linux permissions as Streams (metadata) attached to the files you can see in Windows. Microsoft does not recommend to modify linux files using Windows programs. It is possible that some Windows applcations damage the linux permissions without notice it.
Источник
Linux Home Directory
By Priya Pedamkar
Introduction of Linux Home Directory
In the Linux ecosystem, the home directory is also called as the home directory. It is the primary entry point of the user when they are login into the Linux environment. It is responsible to store files, folders, data, and software on /home directory with the respective individual user profile.
Syntax of Home Directory
Web development, programming languages, Software testing & others
/home [ User Name ]
- /home: We can use the /home as the default path for Linux Home Directory. It is the primary or starting path for entering the individual user profile.
- USER NAME: We need to pass the user name in the “/home” path. It will help to enter in the individual user profile path (in terms of the file system).
How Linux Home Directory Command Works?
When we are creating any user in the Linux system. While creating the user 5 different steps will happen. The user directory creation it comes under the same 5 different steps. When any user will be added in the Linux operating system, by default, the user directory will create in the “/home” path with the same user name.
The user directory is also known as the user home directory. It will provide the basic environment of shell and bash. It will help to execute the shell or some application-level jobs.
By default, the list of files will create in the user “/home” directory.
- bash_logout: The file is responsible to perform any action when the user or the terminal will logout.
- bash_profile: The file is responsible to perform any action when the user profile will load the in the Linux environment.
- Bashrc: The file is responsible for the necessary kinds of stuff in it. It will help to the bash or shell or other applications. We can put functions, alias, shell or bash options etc.
As we have seen, the default home directory path will present in the “/home” path. But is not mandatory that, it will always be present over there. As per the requirement, we can change the home directory for the individual user. We can change the default home directory with the help of “usermod” command (as per user level).
Examples to Implement Linux Home Directory
Following are the examples are given below:
1. Home Directory
When we are creating any user on the Linux level, the user home directory will automatically create in the “/home” path with the same user name.
Источник
Get home directory by username
I want to obtain home dir of any user with echo
But I cant use variable
7 Answers 7
You can use eval :
But see Andrew's comment and glenn's reply below.
$username it's okay, but in sh eval is needed if is a variable
This might work for you:
This will also work on users that are not you. For instance,
It seems you are that user -- why not
There is a safe way to do this!
on Linux/BSD/macOS/OSX without sudo or root
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
See: help printf
Compare to how other answers respond to code injection
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:
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:
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.
Источник