Find the answer to your Linux question:
Results 1 to 2 of 2
I need to create a script that will list all the home directories of all currently logged on users. I have tried the following $ cat /etc/passwd | awk -F: ...
  1. #1
    GML
    GML is offline
    Just Joined!
    Join Date
    May 2008
    Posts
    2

    Home directories script

    I need to create a script that will list all the home directories of all currently logged on users. I have tried the following

    $ cat /etc/passwd | awk -F: '{print $1 "\t" $6}' | sort > user_store.txt
    $ cat user_store.txt

    This is supposed to save the first and sixth field in /etc/passwd (usually username and home directory) and then output it to the screen but having some problems.

    Any help would be appreciated.

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    I'm not at a Linux box but you will need to cross reference this with the who command for logged on users. Try this with a for loop to get it all done. This is just off the top of my head to get you started, I'd recommend reading up some of the man pages to get your further.
    Code:
    for LOGIN in $(who |awk -F: '{print $1}' )
    do grep $LOGIN /etc/password |awk -F: '{ print $1 \t $6 }' |tee user_store
    done
    I haven't included sorting, just wanted to get you started. The tee command outputs to both screen and to the named file but I like to use it as it plays better with for loops than redirection.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...