0

Problem..{ I've users with home dirs in /home, so I can get a list of users matching my criteria with following command.

STEP 1: Fetching A List Of Users

du -h --max-depth=1 /home 2>/dev/null | egrep -v '(!G|.cp)' | awk -F"/" '{print $NF}'

Note: The extended grep pattern is my requirement because home dirs contain extra files that need be inversely greped, Also the !G symbolizes that I need a list of partial users those have data less than 1G.

STEP 2: Fetching A List Of Suspended Users

ls -al /somedir/ | grep -v 'lock'

STEP 3: Need To Compare Those 2 Lists & Get Lines In File 1, that are not present in File 2

So I'm stuck on this step, I've tried uniq & diff command, but I'm not getting list of users meeting my criteria.

PS: The command that should compare both files should not compare these files line against line, but rather overall, so any line that is in file 1 & missing in file 2 should be the output for me.

  • What does the filenames of the suspended users' lock files look like? – Kusalananda Jul 2 '18 at 13:22
  • @Kusalananda it was to skip lock files, cause in that directory there are 2 files associated with single user, so I want userame just one time, so inverse greping does the trick for me. But it's not that important. I want to read file 1 which is output of first command. And file 2 output of 2nd. Compare them and send all entries in file 1 that do not exist in file2. – Sollosa Jul 2 '18 at 13:43
1

Ok I researched a little bit and the solution I was looking for is this

comm -23 <(sort File1) <(sort File2)
| improve this answer | |
1

Use parentheses to group the two command as one:

( du -h --max-depth=1 /home 2>/dev/null | egrep -v '(!G|.cp)' | awk -F"/" '{print $NF}' ; ls -al /somedir/ | grep -v 'lock' ; ) | sort | uniq

The output of the two commands in the parenthese is concatenated and appears as the output of one single process (the subshell).

PS: Your first command outputs /home among the directories...

| improve this answer | |
  • I want all words in file1 that are not in file2 and want to skip remaining ones. Don't want to fetch anything common at all. – Sollosa Jul 2 '18 at 12:44
1

Rather than going through two generated files, iterate over the longer list of usernames and test them against the other directory:

for userpath in /home/*/; do
    user=$( basename "$userpath" )
    if [ ! -e "/somedir/$user" ]; then
        printf 'User %s is not suspended\n' "$user"
    fi
done

This would iterate over all subdirectories in /home and test each directory's basename (the username) against names in /somedir. If there is no corresponding name in that other directory, the user is not suspended.

| improve this answer | |
0

If you want to list all the lines/words in a file that are not in the other:

grep -v -f {smallfile} {bigfile}
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.