Walker News

Get Linux Ls Command To Show File Timestamp In Detail Of Seconds

There is a shell script scheduled to run as cronjob in RHEL 4 server. During trigger time, the Linux shell script executes a C++ program and wait 30 seconds for the program output redirects to a regular file.

If the expected output file doesn’t generated within 30 seconds, the shell script gives up and exit from while loop.

On the other hand, the C++ program inbuilt a variable time-out counter that affected by a number of factors. As such, the C++ program definitely generates output file after hitting its own time-out threshold, which apparently more than 30 seconds fixed in the shell script.

Now, the question is just how many seconds the C++ program took to reach its own time-out counter? In other words, after how many seconds exactly the output file was created?

By default, the Linux ls command either shows date or date and time, depending on the “age” of file:

How to use the Linux ls command?

However, the ls command has option switches to overwrite its default file listing result as you wish:

Use the Linux ls command to show file timestamp with detail of second.
Ignore the funny part of date shown in 2nd screenshot. In fact, I just realize this weird value caused by mistake of using %M (indicate minute). The correct alias should be:
alias lt="ls --time-style='+%d-%m-%Y %H:%M:%S' -l "

The main point here is, however, to highlight the value of “second” shown in file timestamp.

As you can see, the combination of -l and --time-style option switches force the Linux ls command to display file timestamp in detail of second.

According to ls manual page, –time-style takes five possible values, viz full-iso, long-iso, iso, locale, and +FORMAT, where +FORMAT is sharing same convention used in GNU date command.
In RHEL 4, both the ls and date command are of GNU Coreutils package.

Therefore, these are the possible ways to use the –time-style option switch:
ls --time-style="full-iso" -l mbox
ls --time-style="long-iso" -l mbox
ls --time-style="iso" -l mbox
ls --time-style="locale" -l mbox
ls --time-style="+%d-%m-%y %H:%M:%S" -l mbox

If –time-style looks clumsy, there is this alternative option switch --full-time, which is relatively shorter:
ls --full-time -l mbox

To make it even neat and easy to use, just create an Linux alias command as I do, mapping user-defined command name to the ls command and option switch.

Custom Search

  1. Bob 23-12-10@02:49

    Thank you for this guide. I’m having a problem with the full-time on one of my systems, for some reason it seems that system is not recording time stamps out the the ms rage (see example below) does anyone have any ideas how to resolve this?

    Behaving as expected:

    -bash-3.00$ ls -1t --full-time
    -rw-r--r--  1 user group  226233 2010-12-21 23:15:36.000149000 +0000 filename1
    -rw-r--r--  1 user group  226800 2010-12-21 23:15:36.000119000 +0000 filename2
    

    Not behaving as expected:

    -bash-3.00$  ls -1t --full-time
    -rw-r--r--  1 user group  226800 2010-12-21 18:36:56.000000000 +0000 filename1
    -rw-r--r--  1 user group  226800 2010-12-21 18:36:56.000000000 +0000 filename2
    
  2. Walker 23-12-10@11:13

    Maybe the timestamp is exactly at 0 nano-second.

    Please run either one of these commands to see if your system actually record timestamp in detail:

    ls -l --full-time /proc/net/
    
    find / -exec ls -l --time-style='+%N' {} \; | awk '{if ($6 != '000000000'){print $0}}'
    

2014  •  Privacy Policy