Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
Can someone lead me how to do this. I have a bunch of users having .qmail in their home directory. How can I grep or cat whats inside in each ...
  1. #1
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101

    how to grep all users's certain file

    Can someone lead me how to do this. I have a bunch of users having .qmail in their home directory. How can I grep or cat whats inside in each of the user's .qmail file? I'm thinking of a bash script with 'for' syntax. Here is the sample:

    #!/bin/bash

    users=`cat /etc/passwd | awk -F: '{print $1}'`

    for id in $users
    do
    cd ~$id/ | cat .qmail
    done
    Now that I got all the list of users, didn't know how to cat/grep the .qmail file in each users. Please advise.


    Thanks!

  2. #2
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    cat $(IFS=$'\n';printf "%s/.qmail " $(cut -d: -f6 /etc/passwd)) 2>&-

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Actually, packets was very, very close to the solution on his own. Just make one tiny adjustment:
    Code:
     #!/bin/bash
    
    users=`cat /etc/passwd | awk -F: '{print $1}'`
    
    for id in $users
    do
    cd ~$id/
    cat .qmail
    done
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Why bother to cd into the directory?

    Code:
    #!/bin/bash
    
    users=`cat /etc/passwd | awk -F: '{print $1}'`
    
    for id in $users
    do
    cat ~$id/.qmail
    done
    Linux User #453176

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    And using cat is redundant, awk is sufficient:

    Code:
    #!/bin/bash
    
    awk -F: '{print $1}' /etc/passwd |
    while read id
    do
      cat ~$id/.qmail
    done
    Regards

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You don't have to cd to the directory. There are all sorts of ways to do what he wants to do. I'm guessing that part of his question was "what's wrong with this script?", and I wanted to give him the minumum necessary to fix it, so that he could tell what was wrong with it, and thus learn a bit more about shell scripting.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    And using cat is redundant, awk is sufficient
    Correct. But when he said
    How can I grep or cat whats inside
    I'm guessing he wasn't asking, "which tool should I use to examine the data?" I'm guessing that he was asking, "Given a particular tool, how do I access the data?" So my extremely simple fix to his script was what I figure was necessary to send him on his way as a happy camper.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by wje_lf View Post
    Correct. But when he said

    I'm guessing he wasn't asking, "which tool should I use to examine the data?" I'm guessing that he was asking, "Given a particular tool, how do I access the data?" So my extremely simple fix to his script was what I figure was necessary to send him on his way as a happy camper.
    wje_lf,

    It was just to show the OP the possibility with awk not to correct you.

    Regards

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Oh, I knew that. :)

    It's just that the two of us read differently what he wants. Between the two of us, I'm sure if he doesn't have what he wants, he'll speak up.

    Such is the magic of electrons and stuff.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  10. #10
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101
    Sorry for not replying.

    I tried all the script but all it says is:

    cat: ~nelsonts/.qmail: No such file or directory

    My home directory nelsonts has .qmail file with ./Maildir/ in it. The reason why I doing this is I want to get all the users and those users that are not active. I would know fast if I can check inside those .qmail files

Page 1 of 2 1 2 LastLast

Posting Permissions

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