views:

204

answers:

4

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.

+1  A: 

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.

Tim Post
`tr + awk` is kind of overkill when you can just set the field separator in `awk`...
Jefromi
Using `cut` is even simpler than `awk` :)
harto
@harto. for his case yes. cut is simpler. But awk is more flexible.
ghostdog74
@tim, i find that most of the time, people use awk for just simple one liners when in actual fact, you can code a whole program just using it without other tools. `grep+tr` certainly is not needed when you can just use awk.
ghostdog74
A: 

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.

mobrule
No, grep is for filtering out certain lines. He wants every line in the file to be a line in the output, but to only display part of it.
Gabe
+2  A: 

/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
ghostdog74
I think it's bad form to give exact answers to homework questions. Isn't it better to just give hints like "use awk or cut to print the 6th field"?
Gabe
well it is not, if i think OP is at least putting in some effort, no matter how wrong his method may be.
ghostdog74
I got the answer with Grep, thanks to everyone's help, actually. It had to be a single command line, so this particular solution wouldn't have been valid.
KChaloux
A: 
sed 's|\(.[^:]*\):\(.[^:]*\):\(.*\):\(.[^:]*\):\(.[^:]*\)|\4|' /etc/passwd|sort