Display time in ubuntu terminal

Posted on 2011-05-11
Last Modified: 2012-05-11
Hi experts,

How can I display time (and date if possible) in Ubuntu terminal, while a compilation process is going on; ie; for each line, I need to display time.

How can I accomplish this?

I need to do this, since I normally have compilations which will long for hours. So, I need to precisely know at what time the compilation gets over. Else, I have to constantly look at the terminal screen without taking my eyes off the screen!

Any help in this regard will be well appreciated.

Best Regards,
Question by:Sreejith22
    LVL 6

    Expert Comment

    You could create your own tool that add date and time to be start of each line it reads and then pipe the output from your build command through it.
    There might be available tools that does just this, but it's so easy to make it's not worth googling for even :-)

    $ make 2>&1 | ./add-timestamp

    Example add-timestamp.c  --
    /* build:  gcc -o add-timestamp add-timestamp.c */
    #include <stdio.h>
    #include <sys/time.h>
    #include <time.h>

    int main(void) {

            char buf[4097];
            char stamp[20];

            struct timeval tv;
            struct timezone tz;

            time_t lt;
            struct tm *t;

            while ( fgets(buf, 4096, stdin) != NULL ) {
                    lt = (time_t) tv.tv_sec;
                    t = localtime(&lt);
                    strftime(stamp, 20, "%F %H:%M:%S", t);
                    printf("%s: %s\n", stamp, buf);
    return 0;


    Author Comment

    This is the file I use for compilation. I give the following command to compile ./  . How should I incorporate the code you gave, in order to view the time stamp . Where(in which directory) should I place the C file? How should I modify the build command?

    #! /bin/bash
    # ./
    # ./ amss
    # ./ androidu showcommands -j1 V=1
    if [ -d "$PWD/AMSS/$OPERATOR" ]; then
    	./ AMSS
    	if [ -d "$PWD/LINUX/android/$OPERATOR" ]; then
    		./ LINUX
    ./ Gingerbread ${@:-androidu}
    if [[ -d "$PWD/LINUX/android/$OPERATOR" || -d "$PWD/AMSS/$OPERATOR" ]]; then
    	./ restore
    echo -e "\n"
    read -p "Press ENTER to exit..."

    Open in new window

    Any help which would lead me to the correct solution will be well appreciated with p;oints for sure.
    LVL 6

    Accepted Solution

    You can store the C file anywhere you like.

    Store the c file in /var/tmp then, assuming you have gcc installed, issue the following commands (dont actually type the $ nor # prefix, its there to symbolize the command prompt);

    $ cd /var/tmp
    $ gcc -o add-timestamp add-timestamp.c

    You should now have a binary executable in /var/tmp called add-timestamp
    For easy access copy this executable to a directory that is included in the PATH variable. E.g /usr/local/bin

    $ su -
    (enter the root password when prompted)
    # cp /var/tmp /usr/local/bin
    # exit

    Launch your build command as usual, but now also add this " 2>&1 | add-timestamp" at the end of the command.
    $ ./ 2>&1 | add-timestamp

    The terminal output (both stdout and stderr) from and whatever commands/scripts it launches should now be caught by 'add-timestamp' that will prefix each line with the date and time before writing it to the terminal.
    LVL 6

    Expert Comment

    Ops :-) Read me first!
    the line that says
    # cp /var/tmp /usr/local/bin

    in the above comment, should say;

    # cp /var/tmp/add-timestamp /usr/local/bin

    (not sure if/how to edit comments in this place :-))
    LVL 8

    Expert Comment

    For a "good makefile" that uses $(CC), you can try:
      make CC="date ; gcc" -s

    Author Comment

    Will try this and update.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I recently asked a question ( about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
    The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. Once you open the link you will see …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now