How to lists all users who logged in on a specific day using grep and last
up vote
-1
down vote
favorite
I need to write a command or sequence of commands that lists all users who logged in on a monday by using last and grep. I came up with this but it didnt work:
grep -i /var/log/wtmp | last -t 20150731
ubuntu terminal grep last
add a comment |
up vote
-1
down vote
favorite
I need to write a command or sequence of commands that lists all users who logged in on a monday by using last and grep. I came up with this but it didnt work:
grep -i /var/log/wtmp | last -t 20150731
ubuntu terminal grep last
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I need to write a command or sequence of commands that lists all users who logged in on a monday by using last and grep. I came up with this but it didnt work:
grep -i /var/log/wtmp | last -t 20150731
ubuntu terminal grep last
I need to write a command or sequence of commands that lists all users who logged in on a monday by using last and grep. I came up with this but it didnt work:
grep -i /var/log/wtmp | last -t 20150731
ubuntu terminal grep last
ubuntu terminal grep last
edited Nov 24 at 20:43
Rui F Ribeiro
38.3k1475126
38.3k1475126
asked Sep 4 '15 at 1:39
Amjad Allobadi
114
114
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
last | grep -i Mon
(although the -i
option shouldn't be needed if you capitalize "Mon" as I have it)
Your proposed solution has a few problems:
The pipe
|
symbol sends (pipes) the standard output of the first program (on the left) to the second program (as standard input). So what you're doing is trying to pipe the output ofgrep -i /var/log/wtmp
tolast -t 20150731
. Sincelast
outputs the login info, it needs to come first.grep -i /var/log/wtmp
tries to find "/var/log/wtmp" in standard input (case insensitive). It is possible to havegrep
search a file, but the file comes after whatever you're searching for (e.g.,grep find_me in_this_file
searches the filein_this_file
for the string "find_me"). Since no file is specified, it looks in standard input. Withlast
first (before the pipe), that's the output oflast
; but withgrep
first, that's whatever you type after you hit Enter to execute the command.In this case, you actually need to use
grep
to search for "Mon", not "/var/log/wtmp".last
uses "/var/log/wtmp", but allgrep
cares about is the output oflast
, not wherelast
gets its data from.last -t 20150731
would output all logins up to (but not including) July 31, 2015. (To include July 31, you'd need to specify August 1 as the date.) Only problem is thatlast
requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens inlast -t 2015-07-31
but not inlast -t 20150731000000
(both of which specify the same time, midnight beginning July 31, 2015). Your initial question doesn't specify a need to limit the list by date, so I've left it out of my answer above; if you do need to limit by date, uselast -t 2015-07-31
to list through July 30 (not including July 31) orlast -t 2015-08-01
through July 31.
For more details (including all the fun stuff grep
can do with regular expressions and the different time formats for last
), try checking the man pages with man grep
and man last
.
The above displays logins in reverse chronological order (most recent first). Although it goes beyond the constraints of your original question by adding another command, you can also display the results in chronological order (most recent last) by piping the results of grep
to the tac
command: last | grep -i Mon | tac
.
add a comment |
up vote
0
down vote
last | grep Mon | awk '{print $1}' | sort -u
2
No point pipinggrep
toawk
:last | awk '/Mon/ {print $1}'
...
– jasonwryan
Sep 4 '15 at 3:54
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
last | grep -i Mon
(although the -i
option shouldn't be needed if you capitalize "Mon" as I have it)
Your proposed solution has a few problems:
The pipe
|
symbol sends (pipes) the standard output of the first program (on the left) to the second program (as standard input). So what you're doing is trying to pipe the output ofgrep -i /var/log/wtmp
tolast -t 20150731
. Sincelast
outputs the login info, it needs to come first.grep -i /var/log/wtmp
tries to find "/var/log/wtmp" in standard input (case insensitive). It is possible to havegrep
search a file, but the file comes after whatever you're searching for (e.g.,grep find_me in_this_file
searches the filein_this_file
for the string "find_me"). Since no file is specified, it looks in standard input. Withlast
first (before the pipe), that's the output oflast
; but withgrep
first, that's whatever you type after you hit Enter to execute the command.In this case, you actually need to use
grep
to search for "Mon", not "/var/log/wtmp".last
uses "/var/log/wtmp", but allgrep
cares about is the output oflast
, not wherelast
gets its data from.last -t 20150731
would output all logins up to (but not including) July 31, 2015. (To include July 31, you'd need to specify August 1 as the date.) Only problem is thatlast
requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens inlast -t 2015-07-31
but not inlast -t 20150731000000
(both of which specify the same time, midnight beginning July 31, 2015). Your initial question doesn't specify a need to limit the list by date, so I've left it out of my answer above; if you do need to limit by date, uselast -t 2015-07-31
to list through July 30 (not including July 31) orlast -t 2015-08-01
through July 31.
For more details (including all the fun stuff grep
can do with regular expressions and the different time formats for last
), try checking the man pages with man grep
and man last
.
The above displays logins in reverse chronological order (most recent first). Although it goes beyond the constraints of your original question by adding another command, you can also display the results in chronological order (most recent last) by piping the results of grep
to the tac
command: last | grep -i Mon | tac
.
add a comment |
up vote
1
down vote
last | grep -i Mon
(although the -i
option shouldn't be needed if you capitalize "Mon" as I have it)
Your proposed solution has a few problems:
The pipe
|
symbol sends (pipes) the standard output of the first program (on the left) to the second program (as standard input). So what you're doing is trying to pipe the output ofgrep -i /var/log/wtmp
tolast -t 20150731
. Sincelast
outputs the login info, it needs to come first.grep -i /var/log/wtmp
tries to find "/var/log/wtmp" in standard input (case insensitive). It is possible to havegrep
search a file, but the file comes after whatever you're searching for (e.g.,grep find_me in_this_file
searches the filein_this_file
for the string "find_me"). Since no file is specified, it looks in standard input. Withlast
first (before the pipe), that's the output oflast
; but withgrep
first, that's whatever you type after you hit Enter to execute the command.In this case, you actually need to use
grep
to search for "Mon", not "/var/log/wtmp".last
uses "/var/log/wtmp", but allgrep
cares about is the output oflast
, not wherelast
gets its data from.last -t 20150731
would output all logins up to (but not including) July 31, 2015. (To include July 31, you'd need to specify August 1 as the date.) Only problem is thatlast
requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens inlast -t 2015-07-31
but not inlast -t 20150731000000
(both of which specify the same time, midnight beginning July 31, 2015). Your initial question doesn't specify a need to limit the list by date, so I've left it out of my answer above; if you do need to limit by date, uselast -t 2015-07-31
to list through July 30 (not including July 31) orlast -t 2015-08-01
through July 31.
For more details (including all the fun stuff grep
can do with regular expressions and the different time formats for last
), try checking the man pages with man grep
and man last
.
The above displays logins in reverse chronological order (most recent first). Although it goes beyond the constraints of your original question by adding another command, you can also display the results in chronological order (most recent last) by piping the results of grep
to the tac
command: last | grep -i Mon | tac
.
add a comment |
up vote
1
down vote
up vote
1
down vote
last | grep -i Mon
(although the -i
option shouldn't be needed if you capitalize "Mon" as I have it)
Your proposed solution has a few problems:
The pipe
|
symbol sends (pipes) the standard output of the first program (on the left) to the second program (as standard input). So what you're doing is trying to pipe the output ofgrep -i /var/log/wtmp
tolast -t 20150731
. Sincelast
outputs the login info, it needs to come first.grep -i /var/log/wtmp
tries to find "/var/log/wtmp" in standard input (case insensitive). It is possible to havegrep
search a file, but the file comes after whatever you're searching for (e.g.,grep find_me in_this_file
searches the filein_this_file
for the string "find_me"). Since no file is specified, it looks in standard input. Withlast
first (before the pipe), that's the output oflast
; but withgrep
first, that's whatever you type after you hit Enter to execute the command.In this case, you actually need to use
grep
to search for "Mon", not "/var/log/wtmp".last
uses "/var/log/wtmp", but allgrep
cares about is the output oflast
, not wherelast
gets its data from.last -t 20150731
would output all logins up to (but not including) July 31, 2015. (To include July 31, you'd need to specify August 1 as the date.) Only problem is thatlast
requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens inlast -t 2015-07-31
but not inlast -t 20150731000000
(both of which specify the same time, midnight beginning July 31, 2015). Your initial question doesn't specify a need to limit the list by date, so I've left it out of my answer above; if you do need to limit by date, uselast -t 2015-07-31
to list through July 30 (not including July 31) orlast -t 2015-08-01
through July 31.
For more details (including all the fun stuff grep
can do with regular expressions and the different time formats for last
), try checking the man pages with man grep
and man last
.
The above displays logins in reverse chronological order (most recent first). Although it goes beyond the constraints of your original question by adding another command, you can also display the results in chronological order (most recent last) by piping the results of grep
to the tac
command: last | grep -i Mon | tac
.
last | grep -i Mon
(although the -i
option shouldn't be needed if you capitalize "Mon" as I have it)
Your proposed solution has a few problems:
The pipe
|
symbol sends (pipes) the standard output of the first program (on the left) to the second program (as standard input). So what you're doing is trying to pipe the output ofgrep -i /var/log/wtmp
tolast -t 20150731
. Sincelast
outputs the login info, it needs to come first.grep -i /var/log/wtmp
tries to find "/var/log/wtmp" in standard input (case insensitive). It is possible to havegrep
search a file, but the file comes after whatever you're searching for (e.g.,grep find_me in_this_file
searches the filein_this_file
for the string "find_me"). Since no file is specified, it looks in standard input. Withlast
first (before the pipe), that's the output oflast
; but withgrep
first, that's whatever you type after you hit Enter to execute the command.In this case, you actually need to use
grep
to search for "Mon", not "/var/log/wtmp".last
uses "/var/log/wtmp", but allgrep
cares about is the output oflast
, not wherelast
gets its data from.last -t 20150731
would output all logins up to (but not including) July 31, 2015. (To include July 31, you'd need to specify August 1 as the date.) Only problem is thatlast
requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens inlast -t 2015-07-31
but not inlast -t 20150731000000
(both of which specify the same time, midnight beginning July 31, 2015). Your initial question doesn't specify a need to limit the list by date, so I've left it out of my answer above; if you do need to limit by date, uselast -t 2015-07-31
to list through July 30 (not including July 31) orlast -t 2015-08-01
through July 31.
For more details (including all the fun stuff grep
can do with regular expressions and the different time formats for last
), try checking the man pages with man grep
and man last
.
The above displays logins in reverse chronological order (most recent first). Although it goes beyond the constraints of your original question by adding another command, you can also display the results in chronological order (most recent last) by piping the results of grep
to the tac
command: last | grep -i Mon | tac
.
answered Sep 4 '15 at 2:39
j_foster
1536
1536
add a comment |
add a comment |
up vote
0
down vote
last | grep Mon | awk '{print $1}' | sort -u
2
No point pipinggrep
toawk
:last | awk '/Mon/ {print $1}'
...
– jasonwryan
Sep 4 '15 at 3:54
add a comment |
up vote
0
down vote
last | grep Mon | awk '{print $1}' | sort -u
2
No point pipinggrep
toawk
:last | awk '/Mon/ {print $1}'
...
– jasonwryan
Sep 4 '15 at 3:54
add a comment |
up vote
0
down vote
up vote
0
down vote
last | grep Mon | awk '{print $1}' | sort -u
last | grep Mon | awk '{print $1}' | sort -u
answered Sep 4 '15 at 2:06
Gazwald
11
11
2
No point pipinggrep
toawk
:last | awk '/Mon/ {print $1}'
...
– jasonwryan
Sep 4 '15 at 3:54
add a comment |
2
No point pipinggrep
toawk
:last | awk '/Mon/ {print $1}'
...
– jasonwryan
Sep 4 '15 at 3:54
2
2
No point piping
grep
to awk
: last | awk '/Mon/ {print $1}'
...– jasonwryan
Sep 4 '15 at 3:54
No point piping
grep
to awk
: last | awk '/Mon/ {print $1}'
...– jasonwryan
Sep 4 '15 at 3:54
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f227409%2fhow-to-lists-all-users-who-logged-in-on-a-specific-day-using-grep-and-last%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown