Is there a way to determine for example that the current day is the first login for this month? I'm trying to use it in my script. I am familiar with last
but it does not seem to be the command for this job.
Let's take this for an example:
- If today is first login of the month, echo "first!"
- else echo "nope"
Sample Scenario:
July 2 is my first login for the month of July, I logged in 5 times that day. It must only echo "first" on my first login and "nope" on the subsequent 4.
Best Answer
Using
last
andawk
last
searches back through the file/var/log/wtmp
(or the file designated by the-f
flag) and displays a list of all users logged in (and out) since that file was created.Per default
/var/log/wtmp
is created on every new month:last -R $USER
returns all entries from/var/log/wtmp
for the given user. The first login is placed at the bottom of the output, therefore theEND
in myawk
command.Example
Breakdown
% last -R "$USER"
see above
perl -ne 'print unless /wtmp\sbegins/ || /^$/'
removes empty lines and
wtmp begins …
awk 'END {print $4,$5,$6}'
prints the fields 4, 5 and 6 from the last line, the default separator is
(a space)