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