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











share|improve this question




























    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











    share|improve this question


























      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











      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 at 20:43









      Rui F Ribeiro

      38.3k1475126




      38.3k1475126










      asked Sep 4 '15 at 1:39









      Amjad Allobadi

      114




      114






















          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 of grep -i /var/log/wtmp to last -t 20150731. Since last 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 have grep search a file, but the file comes after whatever you're searching for (e.g., grep find_me in_this_file searches the file in_this_file for the string "find_me"). Since no file is specified, it looks in standard input. With last first (before the pipe), that's the output of last; but with grep 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 all grep cares about is the output of last, not where last 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 that last requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens in last -t 2015-07-31 but not in last -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, use last -t 2015-07-31 to list through July 30 (not including July 31) or last -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.






          share|improve this answer




























            up vote
            0
            down vote













            last | grep Mon | awk '{print $1}' | sort -u





            share|improve this answer

















            • 2




              No point piping grep to awk: last | awk '/Mon/ {print $1}'...
              – jasonwryan
              Sep 4 '15 at 3:54











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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

























            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 of grep -i /var/log/wtmp to last -t 20150731. Since last 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 have grep search a file, but the file comes after whatever you're searching for (e.g., grep find_me in_this_file searches the file in_this_file for the string "find_me"). Since no file is specified, it looks in standard input. With last first (before the pipe), that's the output of last; but with grep 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 all grep cares about is the output of last, not where last 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 that last requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens in last -t 2015-07-31 but not in last -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, use last -t 2015-07-31 to list through July 30 (not including July 31) or last -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.






            share|improve this answer

























              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 of grep -i /var/log/wtmp to last -t 20150731. Since last 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 have grep search a file, but the file comes after whatever you're searching for (e.g., grep find_me in_this_file searches the file in_this_file for the string "find_me"). Since no file is specified, it looks in standard input. With last first (before the pipe), that's the output of last; but with grep 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 all grep cares about is the output of last, not where last 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 that last requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens in last -t 2015-07-31 but not in last -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, use last -t 2015-07-31 to list through July 30 (not including July 31) or last -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.






              share|improve this answer























                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 of grep -i /var/log/wtmp to last -t 20150731. Since last 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 have grep search a file, but the file comes after whatever you're searching for (e.g., grep find_me in_this_file searches the file in_this_file for the string "find_me"). Since no file is specified, it looks in standard input. With last first (before the pipe), that's the output of last; but with grep 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 all grep cares about is the output of last, not where last 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 that last requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens in last -t 2015-07-31 but not in last -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, use last -t 2015-07-31 to list through July 30 (not including July 31) or last -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.






                share|improve this answer












                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 of grep -i /var/log/wtmp to last -t 20150731. Since last 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 have grep search a file, but the file comes after whatever you're searching for (e.g., grep find_me in_this_file searches the file in_this_file for the string "find_me"). Since no file is specified, it looks in standard input. With last first (before the pipe), that's the output of last; but with grep 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 all grep cares about is the output of last, not where last 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 that last requires hyphens in the date unless you specify the full time (including seconds). So you need hyphens in last -t 2015-07-31 but not in last -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, use last -t 2015-07-31 to list through July 30 (not including July 31) or last -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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 4 '15 at 2:39









                j_foster

                1536




                1536
























                    up vote
                    0
                    down vote













                    last | grep Mon | awk '{print $1}' | sort -u





                    share|improve this answer

















                    • 2




                      No point piping grep to awk: last | awk '/Mon/ {print $1}'...
                      – jasonwryan
                      Sep 4 '15 at 3:54















                    up vote
                    0
                    down vote













                    last | grep Mon | awk '{print $1}' | sort -u





                    share|improve this answer

















                    • 2




                      No point piping grep to awk: last | awk '/Mon/ {print $1}'...
                      – jasonwryan
                      Sep 4 '15 at 3:54













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    last | grep Mon | awk '{print $1}' | sort -u





                    share|improve this answer












                    last | grep Mon | awk '{print $1}' | sort -u






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 4 '15 at 2:06









                    Gazwald

                    11




                    11








                    • 2




                      No point piping grep to awk: last | awk '/Mon/ {print $1}'...
                      – jasonwryan
                      Sep 4 '15 at 3:54














                    • 2




                      No point piping grep to awk: 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


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    サソリ

                    広島県道265号伴広島線

                    Accessing regular linux commands in Huawei's Dopra Linux