Lsdate - list dates down to the second on UNIX systems

Overview

The lsdate command for UNIX displays the last modified date of one or more files down to the exact second in a consistent and hence easily parsable format.

Background

The UNIX ls command with the -l (minus ell) option for long format displays dates in a rather odd manner.

For dates within the last six months of the current date you get a format similar to:

  Jan 11 19:14

For dates over six months ago a format similar to:

  Feb  4  2001

is used. Neither format includes the seconds attribute.

The lsdate command

The lsdate command will display the modification date of one or more files in the following format:

  YYYY/MM/DD HH:MM:SS filename

where YYYY is the four digit year, MM is the two digit month number, DD is the day of the month, HH is the two digit hour (in 24 hour clock notication), MM is the two digit minute, SS the is the two digit second and filename is the name of the file.

Here are some examples:

  2001/02/04 18:15:18 bin
  2002/01/13 03:46:26 boot
  2002/04/23 19:27:46 dev
  2002/01/13 07:27:08 etc
  2001/12/30 20:09:41 home
  2001/02/04 18:14:10 lib

This format is easier to use in scripts.

Compiling the lsdate.c source

Compile the lsdate.c source file to produce an executable as follows:

  cc -o lsdate lsdate.c

Copy the resulting executable to a suitable location such as:

  /usr/local/bin/lsdate

Ensure that the mode on this executable is:

  -rwxr-xr-x

This ensures that all users on the system will be able to run the lsdate command.

Running lsdate

The lsdate command can be run in one of three general ways.

The first method is to run lsdate as follows:

  lsdate --version

to get version information.

The second method is to specify one or more file names on the command line. For example:

  lsdate /etc/passwd

specifies a single file. An example of several file names could be:

  lsdate /etc/services /etc/inetd.conf /etc/group

Of course shell extensions are available so:

  lsdate *

will list the modification date and time of all the files in the current directory.

The third method of running lsdate is to "feed it" file names from stdin (one file name per line). For example:

  find /usr/bin -type f -print | lsdate

will display the modification date and time of all the regular files under the /usr/bin directory.

Return codes

When lsdate is run with the --version option a return code of 2 is generated.

When running lsdate with either one or more file names specified on the command line or having lsdate "fed" via standard input the return code with be either 0 or 1.

A return code of 1 occurs when one or more of the files could not be found (the file does not exist) or whose details could not be accessed due to insufficient priviledges.

If all files are processed normally then lsdate gives a return code of 0.



  Copyright(c) 1996-2004 Andy Cranston