How to print last login time?











up vote
4
down vote

favorite












I'd like to know how to set in .bashrc file to print the last login time of users when logging into another user in terminal. I do know that with who or last, but I'd like something a little more customizable that can be formatted and only runs once per login, preferably to show in a format like so;



last login time [ 2015-02-13 @ 3:50:00 AM ]
user@host /path/dir/here $


that gets printed to the terminal screen before the bash prompt, and if possible to set and format color on the time and date stamp. How can I achieve this to customizing the bash prompt output?










share|improve this question




























    up vote
    4
    down vote

    favorite












    I'd like to know how to set in .bashrc file to print the last login time of users when logging into another user in terminal. I do know that with who or last, but I'd like something a little more customizable that can be formatted and only runs once per login, preferably to show in a format like so;



    last login time [ 2015-02-13 @ 3:50:00 AM ]
    user@host /path/dir/here $


    that gets printed to the terminal screen before the bash prompt, and if possible to set and format color on the time and date stamp. How can I achieve this to customizing the bash prompt output?










    share|improve this question


























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I'd like to know how to set in .bashrc file to print the last login time of users when logging into another user in terminal. I do know that with who or last, but I'd like something a little more customizable that can be formatted and only runs once per login, preferably to show in a format like so;



      last login time [ 2015-02-13 @ 3:50:00 AM ]
      user@host /path/dir/here $


      that gets printed to the terminal screen before the bash prompt, and if possible to set and format color on the time and date stamp. How can I achieve this to customizing the bash prompt output?










      share|improve this question















      I'd like to know how to set in .bashrc file to print the last login time of users when logging into another user in terminal. I do know that with who or last, but I'd like something a little more customizable that can be formatted and only runs once per login, preferably to show in a format like so;



      last login time [ 2015-02-13 @ 3:50:00 AM ]
      user@host /path/dir/here $


      that gets printed to the terminal screen before the bash prompt, and if possible to set and format color on the time and date stamp. How can I achieve this to customizing the bash prompt output?







      linux bash shell bashrc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 30 '17 at 9:54









      Rui F Ribeiro

      38.5k1479128




      38.5k1479128










      asked Feb 12 '15 at 16:57







      user91679





























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          (Just as a guideline, the format is not exactly the same):



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          export PS1="last login time [$ll]"'nh:W$ '


          Edit: if you want last information to be printed only once (wise idea)



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          echo "last login time [$ll]" # adjust to your login messages, fortunes, etc
          export PS1='nh:W$ ' # replace by your favorite prompt





          share|improve this answer























          • This works okay… but this doesn't just print that one time after logging in via terminal, it appears after each command is finished. Perhaps there is a way to improve on this answer so it only runs once after logging in.
            – user91679
            Feb 13 '15 at 8:44










          • There is always space for improvement, that is the goal of SO! (edited)
            – JJoao
            Feb 13 '15 at 9:16


















          up vote
          0
          down vote













          Try putting the following line into your /etc/profile file ...



          echo "your last login:  `last -1 -R $USER` "


          This will limit the results to 1 line and report the user's last login, during the login process. Afterwards it will not re-occur until the next login.






          share|improve this answer























          • I'd recommend the user's personal ~/.bashrc file over the system-wide one, in case other users are not interested in such output. Otherwise, this seems to use the code from JJoao's answer and so should be credited there.
            – Jeff Schaller
            Nov 29 at 19:29










          • I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though.
            – Richard A. Allcorn - rAllcorn
            Dec 5 at 17:37











          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%2f184506%2fhow-to-print-last-login-time%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
          3
          down vote



          accepted










          (Just as a guideline, the format is not exactly the same):



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          export PS1="last login time [$ll]"'nh:W$ '


          Edit: if you want last information to be printed only once (wise idea)



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          echo "last login time [$ll]" # adjust to your login messages, fortunes, etc
          export PS1='nh:W$ ' # replace by your favorite prompt





          share|improve this answer























          • This works okay… but this doesn't just print that one time after logging in via terminal, it appears after each command is finished. Perhaps there is a way to improve on this answer so it only runs once after logging in.
            – user91679
            Feb 13 '15 at 8:44










          • There is always space for improvement, that is the goal of SO! (edited)
            – JJoao
            Feb 13 '15 at 9:16















          up vote
          3
          down vote



          accepted










          (Just as a guideline, the format is not exactly the same):



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          export PS1="last login time [$ll]"'nh:W$ '


          Edit: if you want last information to be printed only once (wise idea)



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          echo "last login time [$ll]" # adjust to your login messages, fortunes, etc
          export PS1='nh:W$ ' # replace by your favorite prompt





          share|improve this answer























          • This works okay… but this doesn't just print that one time after logging in via terminal, it appears after each command is finished. Perhaps there is a way to improve on this answer so it only runs once after logging in.
            – user91679
            Feb 13 '15 at 8:44










          • There is always space for improvement, that is the goal of SO! (edited)
            – JJoao
            Feb 13 '15 at 9:16













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          (Just as a guideline, the format is not exactly the same):



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          export PS1="last login time [$ll]"'nh:W$ '


          Edit: if you want last information to be printed only once (wise idea)



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          echo "last login time [$ll]" # adjust to your login messages, fortunes, etc
          export PS1='nh:W$ ' # replace by your favorite prompt





          share|improve this answer














          (Just as a guideline, the format is not exactly the same):



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          export PS1="last login time [$ll]"'nh:W$ '


          Edit: if you want last information to be printed only once (wise idea)



          ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
          echo "last login time [$ll]" # adjust to your login messages, fortunes, etc
          export PS1='nh:W$ ' # replace by your favorite prompt






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 13 '15 at 8:50

























          answered Feb 12 '15 at 17:22









          JJoao

          7,0041827




          7,0041827












          • This works okay… but this doesn't just print that one time after logging in via terminal, it appears after each command is finished. Perhaps there is a way to improve on this answer so it only runs once after logging in.
            – user91679
            Feb 13 '15 at 8:44










          • There is always space for improvement, that is the goal of SO! (edited)
            – JJoao
            Feb 13 '15 at 9:16


















          • This works okay… but this doesn't just print that one time after logging in via terminal, it appears after each command is finished. Perhaps there is a way to improve on this answer so it only runs once after logging in.
            – user91679
            Feb 13 '15 at 8:44










          • There is always space for improvement, that is the goal of SO! (edited)
            – JJoao
            Feb 13 '15 at 9:16
















          This works okay… but this doesn't just print that one time after logging in via terminal, it appears after each command is finished. Perhaps there is a way to improve on this answer so it only runs once after logging in.
          – user91679
          Feb 13 '15 at 8:44




          This works okay… but this doesn't just print that one time after logging in via terminal, it appears after each command is finished. Perhaps there is a way to improve on this answer so it only runs once after logging in.
          – user91679
          Feb 13 '15 at 8:44












          There is always space for improvement, that is the goal of SO! (edited)
          – JJoao
          Feb 13 '15 at 9:16




          There is always space for improvement, that is the goal of SO! (edited)
          – JJoao
          Feb 13 '15 at 9:16












          up vote
          0
          down vote













          Try putting the following line into your /etc/profile file ...



          echo "your last login:  `last -1 -R $USER` "


          This will limit the results to 1 line and report the user's last login, during the login process. Afterwards it will not re-occur until the next login.






          share|improve this answer























          • I'd recommend the user's personal ~/.bashrc file over the system-wide one, in case other users are not interested in such output. Otherwise, this seems to use the code from JJoao's answer and so should be credited there.
            – Jeff Schaller
            Nov 29 at 19:29










          • I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though.
            – Richard A. Allcorn - rAllcorn
            Dec 5 at 17:37















          up vote
          0
          down vote













          Try putting the following line into your /etc/profile file ...



          echo "your last login:  `last -1 -R $USER` "


          This will limit the results to 1 line and report the user's last login, during the login process. Afterwards it will not re-occur until the next login.






          share|improve this answer























          • I'd recommend the user's personal ~/.bashrc file over the system-wide one, in case other users are not interested in such output. Otherwise, this seems to use the code from JJoao's answer and so should be credited there.
            – Jeff Schaller
            Nov 29 at 19:29










          • I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though.
            – Richard A. Allcorn - rAllcorn
            Dec 5 at 17:37













          up vote
          0
          down vote










          up vote
          0
          down vote









          Try putting the following line into your /etc/profile file ...



          echo "your last login:  `last -1 -R $USER` "


          This will limit the results to 1 line and report the user's last login, during the login process. Afterwards it will not re-occur until the next login.






          share|improve this answer














          Try putting the following line into your /etc/profile file ...



          echo "your last login:  `last -1 -R $USER` "


          This will limit the results to 1 line and report the user's last login, during the login process. Afterwards it will not re-occur until the next login.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 29 at 19:28









          Jeff Schaller

          37.4k1052121




          37.4k1052121










          answered Nov 29 at 19:01









          Richard A. Allcorn - rAllcorn

          312




          312












          • I'd recommend the user's personal ~/.bashrc file over the system-wide one, in case other users are not interested in such output. Otherwise, this seems to use the code from JJoao's answer and so should be credited there.
            – Jeff Schaller
            Nov 29 at 19:29










          • I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though.
            – Richard A. Allcorn - rAllcorn
            Dec 5 at 17:37


















          • I'd recommend the user's personal ~/.bashrc file over the system-wide one, in case other users are not interested in such output. Otherwise, this seems to use the code from JJoao's answer and so should be credited there.
            – Jeff Schaller
            Nov 29 at 19:29










          • I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though.
            – Richard A. Allcorn - rAllcorn
            Dec 5 at 17:37
















          I'd recommend the user's personal ~/.bashrc file over the system-wide one, in case other users are not interested in such output. Otherwise, this seems to use the code from JJoao's answer and so should be credited there.
          – Jeff Schaller
          Nov 29 at 19:29




          I'd recommend the user's personal ~/.bashrc file over the system-wide one, in case other users are not interested in such output. Otherwise, this seems to use the code from JJoao's answer and so should be credited there.
          – Jeff Schaller
          Nov 29 at 19:29












          I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though.
          – Richard A. Allcorn - rAllcorn
          Dec 5 at 17:37




          I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though.
          – Richard A. Allcorn - rAllcorn
          Dec 5 at 17:37


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f184506%2fhow-to-print-last-login-time%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

          List directoties down one level, excluding some named directories and files

          list processes belonging to a network namespace

          List all connected SSH sessions?