How to put the date and time always visible in the terminal

They say that an image is worth a thousand words, that is why before I explain something, I will show you what is the result of the command that I will put later:

terminal_date_time

Notice how in the upper right corner we see the day of the week (Sun, Sunday), the month (Dec), the day (22) as well as the hour, minute, second and year.

It is something that is updated in real time, that is, every second that information is updated, and it will always be in the upper right corner of the terminal.

This is something useful because, we can be editing a file with nano or vi, we can be installing any type of service or managing whatever, and we will not need to stop what we do, execute date in the terminal to know the date or time With this tip that I show you we will always keep it in mind.

To achieve this in a terminal let's put the following:

while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &

This as such is not a simple command or instruction, but rather a union of them ... come on, a script could also be. Explaining it is a bit complex, however I will do my best 🙂

  • while sleep 1; do : This means that every second the following will be executed
  • tput sc : It means that the current position will be saved, that is, the position of what will come next will be saved, it will not be necessary to specify it after once.
  • put cup 0$ (($ (tput cols) -29)) : This may seem complicated however, it is not that difficult. Basically this is what the position says, that is, the upper right corner. The cup parameter specifies the vertical space that will exist, since we put 0 so it means "just above, at the top". Once the vertical position is defined, we can see the horizontal position, which is taken care of by the rest of the parameters, which to put it simply ... calculates the existing columns and makes sure that it is right on the right edge. If you wish, change the number 29 for others and you will notice the difference.
  • data : Well this is simple, date shows us the information we see ... day, month, hour, etc.
  • tput rc : They are tput sc we save the position, now with tput rc we restore it.
  • done : Here we finish everything, what we started with the while.

As you can see, the terminal is undoubtedly a wonderful space, if a command does not do exactly what we want ... we can join several of them and achieve what we want. See each command as a tool, a tool (hammer) cannot make us a beautiful statue, however, by joining this tool (hammer) with others (wood and chisel) we can reach the dream result 🙂

Oh, by the way ... if you want this to always appear in the terminal without having to execute it every time you open a console, you must put it in .bashrc, that is:

echo "while sleep 1;do tput sc;tput cup 0 \$((\$(tput cols)-29));date;tput rc;done &" >> $HOME/.bashrc

Then if you want to remove it, run the following:

sed -i "s/while sleep 1/#while sleep 1/" $HOME/.bashrc

Well, nothing more to add, I hope it has been useful to you

regards

strong

echo "while sleep 1; do tput sc; tput cup 0 \ $ ((\ $ (tput cols) -29)); date; tput rc; done &" >> $ HOME / .bashrc


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Germaine said

    Thanks but it doesn't work for me ... Neither in Konsole nor in Yakuake I did everything as it is in the post. 🙁

    1.    Germaine said

      Sorry ... it was my fault ... now that I rebooted IT WORKS !!!

      1.    KZKG ^ Gaara said

        It was strange to me that it did not work, because while… date and tput are commands from the Bash package 😀

  2.   Germaine said

    I don't know ... something strange happened ... put in a terminal:

    echo "while sleep 1; do tput sc; tput cup 0 $ (($ (tput cols) -29)); date; tput rc; done &" >> $ HOME / .bashrc

    And the result was:

    bash: /home/ghermain/.bashrc: line 115: syntactic error near unexpected `do 'element
    bash: /home/ghermain/.bashrc: line 115: `PS1 = '$ {debian_chroot: + ($ debian_chroot)} [33 [01; 34m] u [33 [01; 32m] @ [33 [01; 32m] h [33 [00m]: [33 [01; 34m] w [33 [00m] $ 'while sleep 1; do tput sc; tput cup 0 64; date; tput rc; done &'

    Then I directly copied the commands you give into the .bashrc at the end and I get several lines with the date and time.

    1.    O_Pixote_O said

      Delete all the lines that you have put in the .bashrc file on the date and manually put the line again without using the echo that if it does not give an error

  3.   Gonzalo said

    Very interesting!

    1.    KZKG ^ Gaara said

      Thanks for reading 🙂

  4.   jack em said

    I've been looking for ways to "decorate" the efficient but cool terminal for a long time, and this command is very good, not as baroque as other solutions, but when I enter a long command there is some confusion. The command eats the date and then the date seems to eat the command. Does anyone know if there is any way that the prompt appears by default one line lower?
    Anyway, thank you!

  5.   Gregory Swords said

    Excellent tip 🙂

    1.    KZKG ^ Gaara said

      Thanks bro 😀

  6.   Oscar said

    Thanks for the tip friend, it works correctly. Regards.

    1.    KZKG ^ Gaara said

      Thanks to you for reading us 🙂

  7.   truko22 said

    Cool 😀
    Or you can create an alias and use it when needed 😀

  8.   James_Che said

    Interesting, I'll try it later

  9.   Juan said

    Very good

  10.   jamin samuel said

    Could you make a post explaining how to install zsh in Debian / Ubuntu / Mint / Elementary and how to configure themes?

    1.    KZKG ^ Gaara said

      Uff, I've never used zsh, sorry 🙁

  11.   O_Pixote_O said

    KZKG ^ Gaara when you do the command it gives an error because I suppose that it tries to recognize the $ that have no value, so that it does not interpret them, putting them \ is fixed.

    echo "while sleep 1;do tput sc;tput cup 0 \$((\$(tput cols)-29));date;tput rc;done &" >> $HOME/.bashrc

    I guess that way there will be no problem, rectify it before I give someone a mistake. Good post, I will use it. Greetings.

    1.    KZKG ^ Gaara said

      Uff right, my mistake 😀
      I already fix it in the post, thank you very much for the correction 🙂

  12.   jvk85321 said

    I modified it and left it like that

    while sleep 1; do tput sc; tput cup 0 $ (($ (tput cols) -16)); date + »% R% d /% m /% Y»; tput rc; done &

    It only shows Hour: Minutes Date with format DD / MM / YYYY

  13.   danyel said

    Excellent friend I worked 100 thanks