Unix [Homework]: Get a list of /home/user/ directories in /etc/passwd

12.1k views Asked by At

I'm very new to Unix, and currently taking a class learning the basics of the system and its commands.

I'm looking for a single command line to list off all of the user home directories in alphabetical order from the /etc/passwd directory. This applies only to the home directories, and not the contents within them. There should be no duplicate entries. I've tried many permutations of commands such as the following:

sort -d | find /etc/passwd /home/* -type -d | uniq | less

I've tried using -path, -name, removing -type, using -prune, and changing the search pattern to things like /home/*/$, but haven't gotten good results once. At best I can get a list of my own directory (complete with every directory inside it, which is bad), and the directories of the other students on the server (without the contained directories, which is good). I just can't get it to display the /home/user directories and nothing else for my own account.

Many thanks in advance.

6

There are 6 answers

3
ghostdog74 On BEST ANSWER

/etc/passwd is a file. the home directory is usually at field/column 6, where ":" is the delimiter. When you are dealing with file structure that has distinct characters as delimiters, you should use a tool that can break your data down into smaller chunks for easier manipulation using fields and field delimiters. awk/cut etc, even using the shell with IFS variable set can do the job. eg

awk -F":" '{print $6}' /etc/passwd | sort

cut -d":" -f6 /etc/passwd |sort

using the shell to read the file

while IFS=":" read -r a b c d e home_dir g 
do 
  echo $home_dir
done < /etc/passwd | sort
0
ghostdog74 On
sed 's|\(.[^:]*\):\(.[^:]*\):\(.*\):\(.[^:]*\):\(.[^:]*\)|\4|' /etc/passwd|sort
4
AudioBubble On

I think the tools you want are grep, tr and awk. Grep will give you lines from the file that actually contain home directories. tr will let you break up the delimiter into spaces, which makes each line easier to parse.

Awk is just one program that would help you display the results that you want.

Good luck :)

Another hint, try ls --color=auto /etc, passwd isn't the kind of file that you think it is. Directories show up in blue.

1
mob On

In Unix, find is a command for finding files under one or more directories. I think you are looking for a command for finding lines within a file that match a pattern? Look into the command grep.

0
Josiah On

I think all this processing could be avoided. There is a utility to list directory contents.

ls -1 /home

If you'd like the order of the sorting reversed

ls -1r /home

Granted, this list out the name of just that directory name and doesn't include the '/home/', but that can be added back easily enough if desired with something like this

ls -1 /home | (while read line; do echo "/home/"$line; done)
0
ReDragon On

I used something like :

ls -l -d $(cut -d':' -f6 /etc/passwd) 2>/dev/null | sort -u

The only thing I didn't do is to sort alphabetically, didn't figured that yet