1

I am the root user and I am setting up a menu for another user to use. This other user will only get this menu.

There are two options that are interlinked: the first option is to search users. The code I got is:

last | awk '{print $1,$4,$5,$6,$7} ' 

I have checked this code and it works, it shows me the usernames and the day they last logged on.

For the second option: I want to be able to set a date, and them delete users who haven't been active since that date, using the output of the above command.

I am using Linux Mint and Vim text editor.

  • A question—i.e. a sentence with a question mark (?)—would greatly help to indicate what you try to ask. – Anthon Dec 5 '14 at 13:41
  • i want to know a way to delete multiple users who haven't logged in for a long period of time – user2995836 Dec 5 '14 at 13:56
  • correction i want to enter a date then delete users who have been inactive since that date – user2995836 Dec 5 '14 at 14:03
0

You can do it like so:

root@host# lastlog -b Num_Days_Since_Last_Login | egrep -v "^Username|Never logged in" | awk '{print $1}' | xargs -i userdel {}

Where Num_Days_Since_Last_Login is an integer number of days since last login...

0

A complete solution :

#!/bin/bash

maxdate=$(date -d "$1" +%s) || exit 1
daysdate=$((($(date +%s)-maxdate)/(3600*24)))

LANG=C lastlog -b $daysdate -u 1000- |
    awk 'NR>1{print $1}' |
    xargs -n1 echo userdel

Test it and remove echo to do it forReal™. Example usage :

./script.sh 'Fri Dec  5 17:00:06 CET 2013'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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