Wednesday, August 15, 2012

How to create a continuous digital clock in the terminal



A digital clock is actually nothing special, most distros include one by default. However, in this article, I will show you how to get a continuous digital clock in the terminal.

The basic command you can use to display date and time in the terminal is "date". When you open the terminal and run "date", you will have an output of the current date and time. To abbreviate the output to display the time only, you can add a tag (like "+%r" or "+%T") to the "date" command.

With "date", you can display the current time. To create a continuous digital clock, we just need a continuous output. One method is to combine the "watch" command with "date". Thus, to get a continuous digital clock in the terminal, you can just open the terminal and run the following command:

watch -n 1 date +%r

You will have this result:


In case you want a clean output in the terminal, you can use the "echo" command to display the output of the date command and placing that command in an infinite loop. By this method, you can also have a continuous digital clock in the terminal that updates every 1 second.

The command you can use is:
 clear; while true; do echo -e \\b\\b\\b\\b\\b\\b\\b\\b`date +%T`\\c ; sleep 1; done  

And the result is:

 In this command, "echo" is to display the output of "date +%T", the "\\b" string is to delete the former output (How many "\\b" will depend on the output, ie: the output of "date +%T" is 8 characters long so you will need at least 8 times of "\\b", if you use "date +%r" whose output is 11 characters long, you will need at least 11 "\\b".) The "\\c" part is to tell "echo" not to create a new line.

Anyway, it's just a geeky way to play with the terminal and some basic commands. If you seriously need to check the time, I recommend you to use other applications than the terminal :D.

1 comment:

  1. Nice!!! I’m surely going to spend having fun exploring this tip this weekend… Thanks for sharing and looking forward for any updates…
    MagnumClock.com

    ReplyDelete