20

I want to list all the users’ directories on the machine. Usually, I will do:

ls -l /home

But I use it in a script that will be deployed on others’ machines and maybe on those machines they don't call it home (e.g. myHome).

So I want to generalize it to ls -l ~. But it just lists my user’s home directory instead of all users’ home directories (basically I want to get a list of the users’ names on the machine).

How can I generalize it?

Stephen Kitt
  • 434,908
lakerda
  • 355
  • 1
  • 4
  • 7
  • 19
    There is in fact, no guarantee that all users' home directories are subdirectories of any one directory. – hobbs Dec 07 '17 at 08:05
  • 17
    @hobbs or that they even exist until the user logs in. This is one of those apparently simple issues that gets complex really quickly. – EightBitTony Dec 07 '17 at 08:06
  • 11
    Keep in mind that ~ is generally the equivalent of /home/user, not of /home or /home/* (which seem to be closer to your intention). – Soron Dec 07 '17 at 09:29
  • 2
    @EightBitTony: ...nor is there any guarantee that the home directory exists at all. For example, on my Ubuntu install, several system users (including nobody) have their home directory set to /nonexistent, which, obviously, does not exist. (Of course, those users also have their password hash set to * and their shell set to /usr/sbin/nologin or /bin/false, so they really can't log in in the normal sense to begin with.) – Ilmari Karonen Dec 07 '17 at 14:25
  • 1
    On the other hand, an (old school) NFS server might serve home directories for users that are not known by name to its OS. – rackandboneman Dec 08 '17 at 01:31

4 Answers4

52

Many systems have a getent command to list or query the content of the Name Service databases like passwd, group, services, protocols...

getent passwd | cut -d: -f6

Would list the home directories (the 6th colon delimited field) of all the users in databases that can be enumerated.

The user name itself is in the first field, so for the list of user names:

getent passwd | cut -d: -f1

(note that it doesn't mean those users can login to the system or their home directory have been created, but that they are known to the system, they can be translated to a user id).

For databases that can't be enumerated, you can try and query each possible user id individually:

getent passwd {0..65535} | cut -d: -f1,6

(here assuming uids stop at 65535 (some systems support more) and a shell that supports zsh's {x..y} form of brace expansion). But you wouldn't want to do that often on systems where the user database is networked (and there's limited local caching) like LDAP, NIS+, SQL... as that could imply a lot of network traffic (and load on the directory server) to make all those queries.

That also means that if there are several users sharing the same uid, you'll only get one entry for each uid, so miss the others.

If you don't have getent, you could resort to perl:

perl -le 'while (@e = getpwent) {print $e[7]}'

for getent passwd ($e[0] for the user names), or:

perl -le 'for ($i=0;$i<65536;++$i) {
  if (@e = getpwuid $i) {print $e[0] ": " $e[7]}}'

for getent passwd {0..65535} with the same caveats.

In shells, you can use ~user to get the home directory of user, but in most shells, that only works for a limited set of user names (the list of allowed characters in user names supported for that ~ expansion operator varies from shell to shell) and with several shells (including bash), ~$user won't work (you'd need to resort to eval when the name of the user is stored in a variable there). And you'd still have to find a way to get the list of user names.

Some shells have builtin support to get that list of usernames.

  • bash: compgen -u would return the list of users in databases that can be enumerated.
  • zsh: the $userdirs associative array maps user names to their home directory (also limited to databases that can be enumerated, but if you do a ~user expansion for a user that is in a non-enumerable database, an entry will be added to $userdirs). So you can do:

    printf '%s => %s\n' "${(kv@)userdirs}"
    

    to list users with their home directory.

    That only works when zsh is interactive though.

  • tcsh, fish and yash are three other shells that can complete user names (for instance when completing ~<Tab> arguments), but it doesn't look like they let you obtain that list of user names programmatically.

19

A better way to list user home directories, is to parse /etc/passwd and extract them from there, and not make any assumptions about where they may be.

And judging by your second comment, you actually want the user names, not their home directories, in which case, /etc/passwd is a better choice anyway. Note that some Linux / UNIX machines will have other user authentication mechanisms configured (e.g. LDAP), and so ultimately, your query is more complex than you might first imagine, but /etc/passwd is a good place to start.

EightBitTony
  • 21,373
9

The short answer:

compgen -u

The medium answer: Since you're using bash, you can list all possible completions for ~ using compgen -A user. That's such a common usage, it can be abbreviated compgen -u. As a shell builtin, compgen does not have its own man page. Instead see bash(1) for documentation and read the section on Programmable Completion.

A more thorough alternative

If you're extremely concerned about portability, you might not even be able to rely on other machines having bash. In that case, do this:

(getent passwd ||
    dscl . -ls /Users dsAttrTypeNative:homeDirectory || 
    nidump passwd  ||
    cat /etc/passwd) 2>/dev/null  |  cut -d: -f6

Explanation The long answer tries everything, so it will work on pretty much any UNIX system, regardless of whether it uses the newer /etc/nsswitch.conf (both GNU/Linux and BSD come with getent), the traditional UNIX passwd flat file, MacOS's Directory Services¹ (dscl), or even the older, cat-themed MacOS X releases and NeXTSTEP (nidump).

Simplicity But, how portable do you need? Unix has many ways of doing things and sometimes simpler suffices. If you have to pick one, I'd recommend getent passwd | cut -d: -f6 for shell scripts.²


Footnote ¹: I haven't used MacOS in a bit, so if someone can confirm for me that I got the syntax right (and the output doesn't include any stray colons that would mess up cut), that'd be great. Thanks.

Footnote ²: What I recommend and what I do may differ. Personally, I'll more often use the traditional cut -d: -f1 /etc/passwd at the command line. After decades of repetition, my fingers can type it while my mind is working on other things.

hackerb9
  • 211
2

What about ls -l ~/..? This lists all the directories in the parent of your home directory.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
MrMunch
  • 29
  • 5
    That only works on systems where all home directories are based in the same directory, which isn’t many (you might think /home is common on Linux systems, but what happens when you run that as root?). Listing home directories is an adventure full of pitfalls if you want to handle all cases you’re likely to come across, see the other answers for details. – Stephen Kitt Dec 07 '17 at 12:39
  • 7
    This is arguably the only answer so far which directly answers OP's perceived problem. However, it doesn't answer OP's underlying problem. – pipe Dec 07 '17 at 13:40
  • 1
    The value in this answer is in understanding what it misses, and why it is not as good as the other higher-voted answers. – Criggie Dec 08 '17 at 08:50